Using pre_get_posts with WP_Query

The simplest way is to add the action right before the query and remove it immediately after. add_action(‘pre_get_posts’, ‘some_function_in_functionsphp’); $my_secondary_loop = new WP_Query(…); remove_action(‘pre_get_posts’, ‘some_function_in_functionsphp’); if( $my_secondary_loop->have_posts() ): while( $my_secondary_loop->have_posts() ): $my_secondary_loop->the_post(); //The secondary loop endwhile; endif; wp_reset_postdata(); EDIT Another technique you can use is to set your own query var and check for that … Read more

How do I exclude a custom taxonomy from the post loop

You would want to use the NOT EXISTS operator along with passing the taxonomy slug, which tells the query not to include any of a chosen category from your custom taxonomy inside the loop. To exclude all posts that are in the taxonomy “fruit” (regardless of fruit kind), here is the snippet: $args = array( … Read more

“tax_query” parameter not working with WP_Query

The tax_query parameter is an array of arrays, not just an array. This: ‘tax_query’ => array( ‘taxonomy’ => ‘video_type’, ‘terms’ => ‘episode’, ‘field’ => ‘slug’, ‘include_children’ => true, ‘operator’ => ‘IN’ ), Should instead be this: ‘tax_query’ => array( array( ‘taxonomy’ => ‘video_type’, ‘terms’ => ‘episode’, ‘field’ => ‘slug’, ‘include_children’ => true, ‘operator’ => ‘IN’ … Read more

Is it necessary to use wp_reset_query() in a WP_Query call?

Hi @janoChen: Simple answer: no. What follows is what the PHP code for the function wp_reset_query() from /wp-includes/query.php in WordPRess v3.0.4 as well as the functions subsequently called. You can see that it’s primarily about in modifying global variables. When you use new WP_Query($args) you will be assigning the return value from values to a … Read more

Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title

Thank you everyone for your help! In the end the query below got me the results I desired – which was to show and sort the posts by a custom field of “publication_date” first – sorting by the date, and if there were multiple of the same date (say, 4 marked June 2013), it would … Read more

Nested meta_query with multiple relation keys

The question was for WordPress 3.0, but just in case someone has the same question for a more recent version, from WordPress Codex: “Starting with version 4.1, meta_query clauses can be nested in order to construct complex queries.” https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters So, that query should work on the current WordPress version.

How can i retrieve default post per page value? from settings->reading. And total number of posts?

It’s saved in an option: $default_posts_per_page = get_option( ‘posts_per_page’ ); Reference: get_option Parameters You can see the keys and values of all available options by manually entering the admin url: /wp-admin/options.php Edit Total number of posts: global $wp_query; $total_posts = $wp_query->post_count;