Updating an intensive wp_query result once daily

I am using transients for this scenarios. Result is saved to database (wp_options table) with expiration set. It’s usable if the slow load time is below some reasonable treshold … one (first) visitor will have to wait that long to render his request, every other will see saved result. $results = get_transient(‘my_results’); if( ! $results … Read more

post_type not working when tag__in is present?

This is not a bug that I have heard of so I would make sure you do not have a plugin or some other code filtering pre_get_posts. I would recommend checking the SQL that is run to try and debug. $query = new WP_Query($args); echo ‘<pre>’; var_dump($query->request); echo ‘</pre>’; Using that you can verify if … Read more

meta_query only check if both value are set

You can use isset to check if the values are set, and then pass them as arguments. Divide your query by 2 parts and use them as needed. // Check if both are set if ( isset( $max_age ) && isset( $min_age ) ) { $meta_query = array( ‘relation’ => ‘AND’, array( ‘key’ => ‘min_age’, … Read more

Get 5 most recent categories

Okay, seems like this is not supported out of the box. At least I didn’t find any arguments you can use for WP_Term_Query (which is the function used behind get_categories()). So I had the following idea retrieve all categories get latest post from each category save category id & post date in an array order … Read more

Order & Orderby clause not working with custom query

Sorting your posts by relevance. The code below will output the posts ordered by relevance I have added some comments for some clarification let me know if it works. function my_custom_query(){ // get only the posts without order them $my_query_posts = new WP_Query( array( ‘numberposts’ => 5 ) ); // check relevance posts foreach( $my_query_posts->posts … Read more

WP_Query to Retrieve Posts from WooCommerce Products that are Only in Catalog

Posted too quick. Found a solution shortly after This works: $params = array( ‘posts_per_page’ => -1, ‘post_type’ => ‘product’, ‘orderby’ => ‘menu-order’, ‘order’ => ‘asc’, ‘fields’ => ‘ids’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘product_visibility’, ‘field’ => ‘term_id’, ‘terms’ => 7, ‘operator’ => ‘NOT IN’) )); $wc_query = new WP_Query($params); $ids = $wc_query->posts; echo ‘<pre>’; … Read more