Secondary Sort (fallback) for WP_Query

It’s a quite simple revision to your current code. You can add multiple “orderby” values separated by a space. ‘orderby’ => ‘meta_value_num title’, This should keep the priority of sorting by your meta value, with a secondary sort of title.

Use WP_Query with have_posts()?

global $wp_query; $original_query = $wp_query; $wp_query = null; $wp_query = new WP_Query( $args ); if ( have_posts() ) : while ( have_posts() ) : the_post(); the_title(); the_excerpt(); endwhile; else: echo ‘no posts found’; endif; $wp_query = null; $wp_query = $original_query; wp_reset_postdata(); http://codex.wordpress.org/Function_Reference/wp_reset_postdata

WP_query to get the first two latest posts, then another loop to get the next three

Run one wp_query for post_per_page = 2 and get the IDs of these 2 posts in an array to be excluded in the next 3 posts needed <?php // The Query $next_args = array( ‘post_type’ => ‘<your_post_type>’, ‘post_status’ => ‘publish’, ‘posts_per_page’=>2, ‘order’=>’DESC’, ‘orderby’=>’ID’, ); $the_query = new WP_Query( $args ); // The Loop if ( … Read more

WP_Query: query posts by ids from array?

You have to use post__in (double underscore) argument, instead of post_in: echo print_r($rel); // Array ( [0] => 63 [1] => 87 ) $args = array( ‘post_type’ => array( ‘post’ ), ‘orderby’ => ‘ASC’, ‘post__in’ => $rel ); $loop = new WP_Query( $args ); If you are unsure why an argument is not working, copy … Read more

Using OR conditions in meta_query for query_posts argument

Use ‘relation’ => ‘OR’ as in the Codex example below: $args = array( ‘post_type’ => ‘product’, ‘meta_query’ => array( ‘relation’ => ‘OR’, /* <– here */ array( ‘key’ => ‘color’, ‘value’ => ‘blue’, ‘compare’ => ‘NOT LIKE’ ), array( ‘key’ => ‘price’, ‘value’ => array( 20, 100 ), ‘type’ => ‘numeric’, ‘compare’ => ‘BETWEEN’ ) … Read more

Query *only* sticky posts

I currently have no posts set as stickies on the website. Which tells me that nothing should show up in the loop. Exactly where you are wrong when passing an empty array to post__in. WordPress has some silly bugs which has no proper workaround and will most probably stay active bugs for a very long … Read more

WP_query parameters for date range

Copied from StackOverflow: WP_Query offers a date_query parameter, allowing you to set a range with before and after. https://developer.wordpress.org/reference/classes/wp_query/#date-parameters $args = array( ‘date_query’ => array( array( ‘after’ => ‘January 1st, 2015’, ‘before’ => ‘December 31st, 2015’, ‘inclusive’ => true, ), ), ); $query = new WP_Query( $args ); See the linked documentation for more details. … Read more

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