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

How to clear the cache?

Have you looked at WP_Object_Cache? If you suspect there is unwanted caching happening in the code that generates the admin panel then you might be able to use functions from the WP_Object_Cache to clear it. WP_Object_Cache is WordPress’ class for caching data which may be computationally expensive to regenerate, such as the result of complex … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)