Sort wp_query of two post types using meta datefield for one and date for the other – possible?

Try this WP_Query arguments: $args = array( ‘post__in’ => $all_ids, ‘meta_query’ => array( ‘relation’ => ‘OR’, array( ‘key’ => ‘_event_date’, ‘compare’ => ‘EXISTS’, ), array( ‘key’ => ‘_event_date’, ‘compare’ => ‘NOT EXISTS’, ‘value’ => ‘dummy_value’ // This is just to ensure this condition gets evaluated. ) ), ‘orderby’ => array( ‘meta_value_num’ => ‘DESC’, ‘date’ => … Read more

Meta query array – same order as specified order in value

Use PHP to sort the results after the query (untested): $product_ids = array( 3624, 1298, 430, 5629, 765 ); $query = new WP_Query( array( ‘meta_query’ => array( ‘relation’ => ‘AND’, array( ‘key’ => ‘product_id’, ‘value’ => $product_ids, ‘compare’ => ‘IN’, ), ), ) ); $posts_by_product_id = array(); $sorted_posts = array(); if ( $query->have_posts() ) { … Read more

new WP_Query with order args – no more distinction between categories

On the template driving your archive page, you can use the query_posts() function to adjust the main query (WordPress’ default main query, not your custom one). query_posts( array( ‘orderby’ => ‘modified’, ‘order’ => ‘ASC’, ) ); Using the pre_get_posts action is the better method, though slightly more complex (untested, would go in theme’s functions.php or … Read more

How to sort posts alphabetically based on a specific parent category

You can use the following code snippet to sort posts alphabetically based on a specific parent category in WordPress: <?php $parent_cat=”Parent Category Name”; $parent_cat_id = get_cat_ID($parent_cat); $args = array( ‘category__in’ => array($parent_cat_id), ‘orderby’ => ‘title’, ‘order’ => ‘ASC’, ‘posts_per_page’ => -1 ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( … Read more