How to know which one is the main query?

WP_Query->is_main_query() method source (which function of same name calls) is very short and simple: function is_main_query() { global $wp_the_query; return $wp_the_query === $this; } The main query is the query that is stored in $wp_the_query global. But what is that global? When WP sets up main query it stores it in two places: $wp_the_query and … Read more

How do I search an array stored in a custom-field using WP_Query?

Searching inside a serialized array is difficult and inefficient– ie. slow. The following pure SQL will do it: SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE ‘daysonair’ AND `meta_value` LIKE ‘%thursday%’ LIMIT 0 , 100 Something like this should get WP_Query to do something similar: $args = array( ‘post_type’ => ‘post’, ‘meta_query’ => array( array( ‘key’ … Read more

Limit WP_Query to only X results (total, not per page)

As you already mentioned, to limit the number of posts retrieved by the query, you can use posts_per_page. This argument works both for get_posts and WP_Query(), for example: $args = array( ‘posts_per_page’ => 5, ); $posts_array = get_posts( $args ); And as stated in the link you provided, no_found_rows = true will end the query … Read more

Counting Posts of a Given Post Type Having a Specific Taxonomy?

Somatic had the cleanest answer, but missed one thing. You should specify the numberposts to be -1 so that it counts them all. Like this: $args = array( ‘post_type’ => ‘artwork’, ‘post_status’ => ‘published’, ‘genre’ => ‘romantic’, ‘numberposts’ => -1 ); $num = count( get_posts( $args ) ); just replace genere with your taxonomy slug … Read more