Tag Info

Hot answers tagged

67

You are right to say: Never use query_posts anymore pre_get_posts pre_get_posts is a filter, for altering any query. It is most often used to alter only the 'main query': add_action('pre_get_posts','wpse50761_alter_query'); function wpse50761_alter_query($query){ if( $query->is_main_query() ){ //Do something to main query } } ...


24

There are two different contexts for loops: main loop that happens based on URL request and is processed before templates are loaded secondary loops that happen in any other way, called from template files or otherwise Problem with query_posts() is that it is secondary loop that tries to be main one and fails miserably. Thus forget it exists. To modify ...


14

The $query variable in the filter represents a WP_Query object, so you shouldn't be passing a new WP_Query object into the method for setting that object's properties. The question you copied code from was incorrectly using the filter, which i feel is the crux of your issue. Yes, tax_query can be used inside a pre_get_posts (or similarly parse_request) ...


11

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() ): ...


10

There is a legitimate scenario for using query_posts($query) and that is (for example): You want to display a list of posts or custom-post-type posts on a page (using a page template) You want to make pagination of those posts work Now why would you want to display it on a page instead of using an archive template? It's more intuitive for an ...


8

If you change your if statement to use $query->is_main_query() like the example on the is_main_query() codex page, does that resolve the issue? The nav menus are created by a query, so usually this problem stems from the pre_get_posts interfering with the menus query. Hence, I wonder whether that check isn't working.


7

I'll take another shot. The following should modify the main query, such that it will include in its loop any posts that belong to no term of the Edition custom taxonomy. add_filter('pre_get_posts','better_editions_archive'); function better_editions_archive( $query ) { if ( $query->is_tax( 'edition' ) && $query->is_main_query() ) { ...


7

Hi @Dalen: As with many things in WordPress there are several ways to do what you want. I'm going to explain one of them. Remove the 'year', 'monthnum' and 'day' Query Variables You can modify the parameters to the query WordPress uses on the archive URLs inside the 'pre_get_posts' hook. Those parameters are captured as an associative array by the ...


7

Here is an approach to account for sticky posts by getting the number of sticky posts (if any) and include that in the calculation posts_per_page parameter: add_action('pre_get_posts', 'ad_custom_query'); function ad_custom_query($query) { if ($query->is_main_query() && is_home()) { // set the number of posts per page ...


7

Late answer as the most upvoted answer will break your query and simply isn't true in some major points. The main WP_Query and it's filters First, WordPress internally uses query_posts() (a thin wrapper around WP_Query that shouldn't be used in themes or plugins) to do a WP_Query. This WP_Query is acting as the main loop/query. This query will run through ...


7

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' ); ...


6

pre_get_posts will run the same query, so both will take same time. But, If you utilize pre_get_posts action you will save one or more SQL queries. Right now, WordPress is running default query and then you run your query with this function which replace the results of the default query (resulting, default query is of no use). Below is how you can move your ...


6

Try using meta_query parameter : add_action('pre_get_posts', 'add_event_date_criteria'); function add_event_date_criteria(&$query) { // We only want to filter on "public" pages // You can add other selections, like here: // - Only on a term page for our custom taxonomy if (!is_admin() && is_tax('event-tag') || ...


6

You can set the taxonomy query for the main query using pre_get_posts: add_action( 'pre_get_posts', 'my_exclude_terms_from_query' ); function my_exclude_terms_from_query( $query ) { if ( $query->is_main_query() /* && whatever else */ ) { $tax_query = array ( array( 'taxonomy' => 'category', ...


5

You might want to remove those counts and replace them with your own. function insert_post_counts($views){ //Run your query to count posts //use wp_cache_set and wp_cache_get to optimize performance $edit_url = admin_url( 'edit.php' ); $views['all'] = 'All <a href="'.$edit_url.'">('.$all_count.')</a>'; $views['publish'] = ...


5

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 ...


5

I know this is an old question, but it is a bit confusing and hopefully will help someone. The reason that `$query->set doesn't work is because the query has already been parsed and now we need to also update the tax_query object also. Here is how I did it: function my_tax_query( $query ) { $package_id = 12345; $tax_query = array( 'taxonomy' ...


4

you can change your search filter to this: function SearchFilter($query) { $post_type = $_GET['post_type']; if (!$post_type) { $post_type = 'any'; } if ($query->is_search) { $query->set('post_type', $post_type); }; return $query; } add_filter('pre_get_posts','SearchFilter'); then on your news search form add : <input type="hidden" ...


4

Firstly, 'pre_get_posts' is an action and not a filter. That's the main problem to start. Secondly, you need to set conditionals for the context. add_action('wp', 'custom_post_count'); function custom_post_count($query){ if($query->is_home || $query->is_front_page){ $query->set('posts_per_page', 5); } return $query; }; The ...


4

Basically what you are looking for is the global $wp_the_query variable which is set to the value of the main query. It may not be a perfect fit for 100% of cases but will probably work fine in 99% of cases: add_action( 'pre_get_posts', 'custom_post_count' ); function custom_post_count( $query ){ global $wp_the_query; if ( $wp_the_query === $query ) { ...


4

Just use the appropriate conditionals: function change_order_for_events( $query ) { //only show future events and events in the last 24hours $yesterday = current_time('timestamp') - 24*60*60; if ( $query->is_main_query() && (is_tax('event_type') || is_post_type_archive('wr_event')) ) { $query->set( 'meta_key', ...


4

Well, you SHOULDN'T use pre_get_posts, as it will alter ALL the queries in the site. Also, the query made by the recent posts plugin is not the main query. The main query is made for displaying the current page, after parsing the query variables from URL, and it is responsible, among other things for the current page template. What you SHOULD do is ...


4

Please check out the answers at When to use WP_query(), query_posts() and pre_get_posts. It is a great ressource if you have any doubts in mind.


4

I see many people prefer to use pre_get_posts hook instead of query_posts Yay! So pre_get_posts filters a WP_Query object which means anything you could do via query_posts() you can do via $query->set() and $query->get(). In particular we can make use of the meta_query attribute (see Codex): $meta_query = array( array( ...


4

Your filter has a bug in it, namely when you call is_main_query, you're not checking if the passed query is the main query, your checking if the currently active query is the main query, which will always be true. So instead try this: add_action( 'pre_get_posts', 'some_name'); function some_name($query) { if ($query->is_front_page() && ...


4

It appears that you want to create a custom page template to display a custom post type? If so, you don't really need to mess with pre_get_posts at all. First, create your custom page template. I assume you've already done this. Second, you need to create a custom query, using WP_Query(): global $post; $types = $post->post_name; $trips_query_args = ...


3

pre_get_posts triggers for every query (front and admin side). It also triggers when querying nav-menu items (which is what is happening here). Most of the time you only want to filter particular queries - so you need to check if that query is one you wish to modify. Usually - and I think your example might be one - you only want to modify the 'main ...


3

pre_get_posts fires for every post query: get_posts() new WP_Query() That random recent posts widget your client installed without you knowing. Everything -- @nacin With that being said unless you exclude your filter use the conditional: is_main_query() then your filter will fire on your new WP_Query. If you only want to target ...


3

You're almost there mate. Try this though. <?php add_action('pre_get_posts', 'filter_press_tax'); function filter_press_tax( $query ){ if( $query->is_tax('press') && $query->has_term('press')): $query->set('posts_per_page', 5); return; endif; } ?> You can use any conditional tag ...


3

This may be overly simplistic, but have you tried changing is_home() to is_category(), in this line: if ( is_home() && false == $query->query_vars['suppress_filters'] ) i.e. change it to this: if ( is_category() && false == $query->query_vars['suppress_filters'] ) Worth a shot, anyway...



Only top voted, non community-wiki answers of a minimum length are eligible