0

I have two queries with arguments and then I merged them to form my new query - $wp_query. I would like to now apply arguments to the new query to show the merged pages/posts by meta_value_num. This ordering works great on the $page_args array if I apply it there but not to the combined array. Also the meta_value_num prints out in the loop, but does not order the posts/pages at all. What am I missing here? Thanks.

$page_args = array(
    'post_type' => array( 'page'),
    'post__in' => get_pageslug(" 'first_page', 'second_page' ")
);

$blog_args = array(
    'post_type'=> array('blog'),
    'showposts' => 1,//latest
    'offset' => 0
);

$query_1 = new WP_Query( $page_args );//5
$query_2 = new WP_Query( $blog_args );//1

$args = array(
    'meta_key' => 'my_ordering',
    'orderby' => 'meta_value meta_value_num',
    'order' => 'ASC'
);

// Create new empty query and populate it with the other two queries to combine
$wp_query = new WP_Query($args);
$wp_query->posts = array_merge( $query_1->posts, $query_2->posts ); 
$wp_query->post_count = count( $wp_query->posts ); //6

while ( $wp_query->have_posts() ) : $wp_query->the_post();
$box_ordering = get_post_meta($post->ID, 'my_ordering', true);
echo $box_ordering . '<br>'; //outputs meta values for each page/post correctly
//but does not order the content by meta value
endwhile;
  • Is there a reason you can't write a single query with one set of args? The meta ordering only applies to the query results when you do the query, if you merge them, they'll be ordered with all the $query_1 posts followed by all the $query_2 posts. – mrwweb Jul 22 '14 at 21:15
  • Well, the idea is that I need to order the combined set of posts by the meta number. The first is for a particular set of pages, and the second query is for the most recent post from a custom post. If combining them works, I would like to know how to do that and still get the correct results with the added featured of being able to order all of the results. Thanks. – amespower Jul 22 '14 at 21:24
  • I have a few ideas. Before that, though, what's the get_pageslug function? That's not in core. (And another thing. showposts is deprecated. Use posts_per_page) – mrwweb Jul 22 '14 at 21:37
  • get_pageslug just gets the id of the post. ah yes, posts_per_page. Would be interested to hear your ideas. Thanks – amespower Jul 22 '14 at 21:45
0

Here's what I'd do. This is mildly tested.

It gets the IDs of all the posts you need and then builds a single query to get the posts in the correct order.

// get your page IDs, I assume this returns an array of integers
$page_ids = get_pageslug(" 'first_page', 'second_page' ");
// get your blog post ID
$blog_ids = new WP_Query( array(
    'post_type'=> array( 'blog' ),
    'posts_per_page' => 1,
    'fields' => 'ids', // just get the ID
    'cache_results' => false // because we're only getting the ID
) );

$my_wp_query_args = array(
    'post_type' => array( 'blog', 'page' ),
    'post__in' => array_merge( $page_ids, $blog_ids->posts ), // merge two arrays of IDs
    'meta_key' => 'my_ordering',
    'orderby' => 'meta_value_num', // meta_value_num is used *in place of* meta_value
    'order' => 'ASC'
);

// NOTE: $wp_query is an internal WP object already so don't overwrite it.
$my_wp_query = new WP_Query( $my_wp_query_args );

while ( $my_wp_query->have_posts() ) : $my_wp_query->the_post();
    $box_ordering = get_post_meta($post->ID, 'my_ordering', true);
    echo $box_ordering . '<br>'; //outputs meta values for each page/post correctly
endwhile;
  • Unfortunately, it returns the same results as before. Thanks for your help. Maybe a custom sql query is the best option. – amespower Jul 23 '14 at 17:46
  • Did you use the exact code above? I'm surprised the results would be identical. – mrwweb Jul 23 '14 at 17:57
  • mrwweb, not sure where I went wrong earlier in incorporating this into my much larger code, but it works perfectly. Thanks so much. – amespower Jul 23 '14 at 18:43
  • Phew! That's great. Make sure to accept the answer if you feel it correctly answered your question. – mrwweb Jul 23 '14 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.