Ordering by meta_key

That happens because of the way WordPress structures the MySQL queries, and the way posts joins postmeta. If a post doesn’t have a value in featured, it won’t join. Here are two options for you: Update the way you’re storing featured to store the value regardless (either ‘1’ or ‘0’). Ensure that every post has … Read more

Remove category from query (show all posts in archive.php) pre_get_posts()

Just unsetting the cat variable probably isn’t enough. The pre_get_posts hook happens after the query variables have already been parsed. So there’s probably a tax_query with the taxonomy = category and the terms = your category. You’re already dumping the $query in your code, presumably for debugging. So, look at what you’re actually dumping. Do … Read more

Injecting a custom sql query into a page/theme

The hooks you are looking for can be found in the WordPress Codex WP_Query – Filters: Filters posts_distinct – Alters SQL ‘DISTINCTROW’ clause to the query that returns the post array. posts_groupby – Alters SQL ‘GROUP BY’ clause of the query that returns the post array. posts_join – Alters SQL ‘JOIN’ clause of the query … Read more

Exclude post by custom meta with pre_get_posts

Your can use the “meta_query” argument of the query, I think is better than filter the post_where: add_action( ‘pre_get_posts’, ‘bbwp_calendar_visible_events’ ); function bbwp_calendar_visible_events( $query ) { $postType=”bbwp_calendar”; if ( !is_admin() && $query->is_main_query() ){ if ( is_post_type_archive( $postType ) ) { $meta_query = array( ‘relation’ => ‘OR’, //current time <= event_end_date array( ‘key’ => ‘event_end_date’, //Check … Read more

Can I set my WP_Query to be a Main Query?

This is an overly complicated approach. pre_get_posts will work for any query and there are numerous ways to control when/how the callback behaves: Add and remove the callback: function dummy_action($q) { } add_action(‘pre_get_posts’,’dummy_action’); $q = new WP_Query( array( ‘post_type’ => ‘post’ ) ); remove_action(‘pre_get_posts’,’dummy_action’); Use a self-removing callback: function dummy_action($q) { remove_action(‘pre_get_posts’,’dummy_action’); } add_action(‘pre_get_posts’,’dummy_action’); $q … Read more

How to achieve post_status__not_in?

Unfortunately there is no post_status__not_in query argument, but you can simply query for all statuses other than trash & draft: $stati = array_keys( get_post_stati() ); unset( $stati[‘trash’], $stati[‘draft’] ); $stati = array_flip( $stati ); $query->set( ‘post_status’, $stati );