What to do in this case after wp_query has been modified

If the code shown above is the one the plugin is using, it will affect every query, since it’s not checking for is_main_query() (https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts) What you could do, is remove the action, do your query, and then add the action again (if needed) remove_action( ‘pre_get_posts’, ‘category_and_tag_archives’ ); $myQuery = new WP_Query($queryArgs); add_action( ‘pre_get_posts’, ‘category_and_tag_archives’ ); … Read more

Where should you reset postdata?

Short version: As Tom J Nowell said, You shouldn’t cleanup if there’s nothing to clean Long version: However, if you put wp_reset_postdata() after (or outside) the loop, it would still work perfectly fine. I have used the function in various scenarios, including something like this: dynamic_sidebar( ‘main-sidebar’ ); wp_reset_postdata(); The reason was that some widget … Read more

paginate_links ignore my format

This part: ‘base’ => str_replace( 999999999, ‘%#%’, esc_url( get_pagenum_link( 999999999 ) ) ), is generating a page part like this one: ‘base’ => http://example.tld/page/%#%/ If we peek into paginate_links() we see the default: ‘base’ => $pagenum_link, // http://example.com/all_posts.php%_% : // %_% is replaced by format (below) ‘format’ => $format, // ?page=%#% : %#% is replaced … Read more

Query Posts in a Predefined Order

If the query is only for a small number of posts, then as linked to by Alex you can sort in php. However, this does not scale well. As suggested by Kovshenin – a better alternative is to use posts_orderby filter: $post_ids = array(83,24,106,2283,14); $args = array( ‘post_type’ => ‘post’, ‘post__in’ => $post_ids, ‘numberposts’ => … Read more

Changing the meta_query of the main query based on custom query_vars and using pre_get_posts

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 query_var_by_date() { add_rewrite_tag( ‘%event_date%’, ‘([0-9]{4}-[0-9]{2}-[0-9]{2})’); } add_action( ‘init’, ‘query_var_by_date’ ); function custom_event_query( $query ) { if ( get_query_var( ‘event_date’ ) && … Read more

How to check if a WP_Query has data

Change it up a bit and use have_posts method to check if there are any results: <?php $args = array( ‘post_type’ => ‘questions’, ‘posts_per_page’ => ‘3’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘types’, ‘field’ => ‘slug’, ‘terms’ => ‘customer-service’ ) ) ); $loop = new WP_Query( $args ); if ($loop->have_posts()){ ?> <h4>Frequently Asked Questions</h4> <ul … Read more

How to Compare Two Meta Fields

A rought method would be to set value of balance to ‘mt1.meta_value’ and then use a ‘posts_request’ filter to remove the escaping quotes: $args = array( ‘post_type’ => ‘post’, // maybe not ‘meta_query’ => array( array( ‘key’ => ‘balance’, ‘compare’ => ‘>=’, ‘value’ => ‘mt1.meta_value’, // this will be treated as a string ‘type’ => … Read more

WP_Query and polylang issue

OK – I got it, the old posts already on the database do NOT have an english translation and that’s the problem with my loop. When I add a new post, I have the option to provide a translation which then appears in the english page. looking through the docs I found that I can … Read more

tech