complex restriction of items in media library

Good code. I think you could simplify by making direct SQL queries instead: — Featured Images SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = ‘_thumbnail_id’; –> $thumbnail_ids — Header Images SELECT `post_id` FROM `wp_postmeta` WHERE `meta_key` = ‘_wp_attachment_context’ and `meta_value` = ‘custom-header’; –> $header_ids Also, with your method, I think that if you call WP_Query with … Read more

Order Search Results Page by meta_value If no Value Return Remaining Results

You can accomplish this with multiple meta queries. $query->set( ‘meta_query’, [ ‘relation’ => ‘OR’, ‘wpcf-start-date’ => [ ‘key’ => ‘wpcf-start-date’, ‘compare’ => ‘EXISTS’, ], ‘no-start-date’ => [ ‘key’ => ‘wpcf-start-date’, ‘compare’ => ‘NOT EXISTS’ ], ] ); $query->set( ‘orderby’, ‘wpcf-start-date’ ); $query->set( ‘order’, ‘ASC’ ); This will tell WP to create a query that will … Read more

Show only posts which can be commented OR have custom meta field

Lets try the following: By default, all posts are returned regardless of comment_status, so lets run the main query as normal, that is, query all posts regardless of comment_status. We will also run a small, but very lean secondary query where we will get all posts which have a comment_status of closed which have a … Read more

pre_get_posts with tax_query causes empty result

You use tax_query incorrectly. Take a look at Codex Page tax_query should be an array which can contain: relation – it should be string (AND/OR) taxonomy term – array with defined taxonomy, field, terms, and so on. In your code your setting tax_query to: $taxquery = array( ‘post_type’ => ‘post’, ‘tax_query’ => array( array( ‘taxonomy’ … Read more

$query->set in pre_get_posts is unintentionally affecting the backend

In regards to your decision not to use WP_Query, pre_get_posts is actually an excellent choice to make, rather than creating a new instance of WP_Query. In fact, pre_get_posts is exactly what you should be using when you want to change the main query. Rather than executing a separate query on each page load, it modifies … Read more

Changing the meta_query of the main query based on custom query_vars and using pre_get_posts

Instead of trying to display all the matching events on your by-date page, you could try to display through ?post_type=event like this: function rewrite_rule_by_date() { add_rewrite_rule(‘by\-date/([0-9]{4}\-[0-9]{2}\-[0-9]{2})$’, ‘index.php?post_type=event&event_date=$matches[1]’, ‘top’); } add_action( ‘init’, ‘rewrite_rule_by_date’ ); function query_var_by_date() { add_rewrite_tag( ‘%event_date%’, ‘([0-9]{4}-[0-9]{2}-[0-9]{2})’); } add_action( ‘init’, ‘query_var_by_date’ ); function custom_event_query( $query ) { if ( get_query_var( ‘event_date’ ) && … Read more

is_category() in pre_get_posts strange error

After a bit of investigation… If you pass a category to is_category it uses get_queried_object to grab data– see the source. get_queried_object returns NULL for categories that do not exist. You can demonstrate that with: function custom_posts_per_page($query) { var_dump(get_queried_object()); } add_filter( ‘pre_get_posts’, ‘custom_posts_per_page’ ); Load a valid category archive and then an invalid one. The … Read more

How to add taxonomy filter on the query fly?

You have a syntax-ish error, or a spelling one. There is a space after “taxonomy” in your tax_query array. If that were the sum of it I’d have just posted a comment, but additionally that $query->tax_query->queries[] = $workspace_taxonomy_query; line is pointless. Nothing changes with or without it. Don’t try to hack the array like that. … Read more