Hot answers tagged

93

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


32

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


18

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


15

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


14

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


12

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


11

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


11

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.


10

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


10

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


10

I finally got it working, but not with the code in my question. I totally scrapped that whole idea and restarted going in a new direction. NOTE: If anyone are ever able to sort out the issues in my question, feel free to post an answer. Also, if you have any other solution, also feel free to post an answer. REWORKED CLASS AND SOLUTION: What I tried to do ...


9

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


8

As of Wordpress 3.7 a new action named parse_tax_query was added exactly for this purpose. function kia_no_child_terms($wp_query) { $wp_query->tax_query->queries[0]['include_children'] = 0; } add_action('parse_tax_query', 'kia_no_child_terms'); This hook modifies the values of both query_vars and tax_query. Using the pre_get_posts method ...


8

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


8

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


8

Adding Custom Sortable WordPress Admin Table Columns by Taxonomy Terms To achieve adding a custom sortable column to the WP_List_Table of your post type within the WordPress administration back-end dashboard, you will need to do the following... Replace all occurrences of "YOUR-POST-TYPE-NAME" with your actual post type name. Replace all occurrences of "...


7

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() && $...


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

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') || is_tax('...


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


6

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


6

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


6

When 'pre_get_posts' is fired, there are a lot of things that WordPress has already done like setup all the query variables (also the query you don't send) and setup all the conditional properties (all the is_* properties). So, if you want completely replace the query you should reset all that things and set your own. In fact, even if your code works, ...


6

As Milo said : $query->set('meta_key', 'wpcf-object-sold-status' ); $query->set('orderby', array('meta_value' => 'ASC', 'date' => 'DESC')); // $query->set('order', 'ASC DESC' ); // not needed Relevant link: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/


5

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


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

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.


5

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


5

This method will return all of the posts including those with and without the requested meta_key, but it will do weird things when ordering. add_action('pre_get_posts', 'my_stuff'); function my_stuff ($qry) { $qry->set( 'meta_query', array( 'relation' => 'OR', # Matches to this meta_query should be added to those ...


5

Improve your conditional to include a check for post type being queried. It can be done via WP_Query::get method So, where you have if ( !is_admin() ){ $query->set( 'meta_key', '_ct_selectbox_52f65ae267764' ); $query->set( 'meta_value', $city ); return; } replace with if ( ! is_admin() && in_array ( $query->get('post_type'),...



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