pre_get_posts post_meta event

If you have store date different then timestamp it will be VERY hard to do this. To get this if is timestamp stored, you need add “meta_query” like this: add_action(‘pre_get_posts’, ‘my_pre_get_posts’); function my_pre_get_posts($query) { $offest = 0;// you must count this to avoid today and tomorrow $query->set( ‘meta_query’, array( ‘relation’ => ‘AND’, array( ‘key’ => … Read more

Pre Get Posts – Difference between conditional() and $query->conditional()?

There’s a big difference… most of the times. is_home(), just like any other conditional tag, check main query. $query->condition() check the query object that is passed by action, that can be a a completely different query, because pre_get_posts is called for all queries. As example, let’s assume you are viewing a singular post, and inside … Read more

Sort WordPress Archive by multiple oderby arguments in pre_get_posts action

I am not an expert, but you have a couple of issues here Firstly, is_archive() should be an object of $query. You should also just target the front end with your function. Remember, pre_get_posts alters all instances of WP_Query, front end and backend. So this line if(is_archive() && $query->is_main_query()) : should look something like this … Read more

Why query by specific date with variables doesn’t return same result that with harcoded integers?

I believe your problem is that you are not casting the variables over into integers. You should give this a try: $query->set(‘date_query’, array( array( ‘year’ => (int)$dies[‘year’], ‘month’ => (int)$dies[‘mon’], ‘day’ => (int)$dies[‘mday’], ), )); or if you would like, before you even set up the arguments for the query, you could do something along … Read more

pre_get_posts to hide everywhere posts from “Archive” category

I’m not sure how your current “hidden” checkbox code is working (there’s an undefined $archive variable, in_category might not fire correctly in the context, and I doubt your post type is actually your_post_type). Just to make sure we get everything working, here is a complete solution for adding a checkbox to the edit post screen, … Read more

Unpublish Authors With not Fulfilled Fields

You’re doing something like this tutorial on categorizing wordpress users, correct? I think you should do your filtering in your theme files, not on a hook. Just add an if statement to your template file (probably author.php) that tests if your custom fields aren’t empty strings, and then do whatever (404, redirect elsewhere, throw a … Read more

Altering the main query using get_post_meta() in pre_get_posts

Per the wordpress codex pre_get_posts() does not work everywhere. is_font_page() will not work but is_home() will. So your condition is_front_page() && is_home() will fail every time. However ‘is_home()’ alone should work. It might be helpful to others to know what exactly you are trying to do. Usually pre_get_posts is used to alter a query but … Read more