How do I force WP_query to fetch fresh, non-cached results?

WP_Query has a Caching Parameters you can configure to return the desired data with/without caching. $swp_args = array( // Show posts without adding post meta information to the cache. ‘update_post_meta_cache’ => false, … ); In your specific case update_post_meta_cache allows you to turn on/off the caching for post meta.

Make (post) query a clickable link

You can add a query string to your URL and then alter the query if that string is set. What I’m going to do is to use pre_get_posts to modify the main query. function sort_by_views($query) { if ( !is_admin() && $query->is_main_query() && $query->is_home() && isset( $_REQUEST[‘sort_by_views’] ) ) { $query->set( ‘post_type’, ‘download’); $query->set( ‘post_status’, ‘publish’ … Read more

How to change main query based on post meta

Finally got it. This code in functions.php: function set_order_by_last_date($query){ $query->set( ‘orderby’, ‘eventstart’); $query->set( ‘order’, ‘DESC’ ); $query->get_posts(); if ($query->have_posts()){ if ( eo_get_the_end(‘Ymd’,$query->posts[0]->ID,$query->posts[0]->occurrence_id) > date(‘Ymd’) ){ $query->set( ‘event_end_after’, ‘today’ ); $query->set( ‘orderby’, ‘eventstart’); $query->set( ‘order’, ‘ASC’ ); } else { $query->set( ‘event_end_before’, ‘today’ ); $query->set( ‘orderby’, ‘eventstart’); $query->set( ‘order’, ‘DESC’ ); } } return $query->query_vars; } … Read more

Query all published post AND attachment with specific mime type

First of all if any is used for post_type field there can not be any other value given in array like in the question. Below is the modified args $args = [ ‘post_type’ => array_values(get_post_types([‘public’ => true])), ‘post_status’ => [‘publish’, ‘inherit’], ‘posts_per_page’ => 10 ]; Use posts_where filter add_filter(‘posts_where’, ‘add_search_mime_types’); $query = new WP_Query( $args … Read more

Querying on multiple taxonomies pulled from $_GET checkbox array not working?

Here: $search_values[‘tax_query’] = array ( You’re resetting the value of tax_query for every item in the foreach loop. It’s also being set to an invalid value: Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays). This construct allows you to query multiple taxonomies by using the relation … Read more

Store sticky post’s ids in a transient

You can try: add_action(‘update_option_sticky_posts’, function( $old_value, $value ) { $featured_args = array( ‘post__in’ => $value, ‘post_status’ => ‘publish’, ‘no_found_rows’ => true ); // The Featured Posts query. $featured = new WP_Query( $featured_args ); // Proceed only if published posts with thumbnails exist if ( $featured->have_posts() ) { while ( $featured->have_posts() ) { $featured->the_post(); if ( … Read more