Support » Fixing WordPress » Combine two queries in to one

  • Resolved David Gard

    (@duck_boy)


    Hey all,

    Just wondering if anyone with a brain bigger than mine can think of a way of combining these two queries in to one?

    $args1 = Array(
    	'posts_per_page' => 10,
    	'paged' => $paged,
    	'post_type' => 'dd_tla'
    );
    query_posts($args1);
    $args2 = Array( // Array of arguments for query_posts()
    	'cat' => $cat,
    	'posts_per_page' => 10,
    	'paged' => $paged,
    	'post_type' => 'post',
    	'tag' => get_query_var('tag'),
    	'tla_tag' => get_query_var('tla_tag'),
    	'contact' => get_query_var('contact')
    );
    query_posts($args2);

    Any hints and tips appriciated!

    Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)
  • esmi

    (@esmi)

    Forum Moderator

    The only real difference in the 2 queries appears to be the post_type. I’ve absolutely no idea if this will work but have you tried using 'post_type' => array('post','dd_tla'),?

    I have indeed tried that, but doesn’t work. I suspect because I need to be very specific with the cats/tags/etc, so it just ignores the posts of that type because they don’t comply to the criteria, hence the need to merge the two seperate queries.

    I am looking at custom queries, but not really savvy enough on SQL to pull it off!

    esmi

    (@esmi)

    Forum Moderator

    Is there any specific need to just run a single query? Or could you run your two queries as they are and then just merge the results into a single array?

    I could do that, wouldn’t effect the page, but don’t know how – thought you could only run one query per page!

    esmi

    (@esmi)

    Forum Moderator

    You can run as many as you like as far as I know – although you may get performance issues if you run a dozen. 🙂

    I’ve just been working on a template that merges the results of two queries. I want x posts on the page, so first I query on sticky posts. If the number returned in the array (y) is less than x, I run a second query for x-y posts in a specific category minus any stickies, merge the two arrays and output the results. That way I always get x posts on the page and the user can control which posts and their rough order.

    I’ve been using get_posts for the 2 queries. If you’re using query_posts, you might want to use wp_reset_query() after each query_post to stop them impacting on any other Loops further down the page. But, other than that, it should work.

    Ok, I’m giving this a bash right now.

    One problem I’m not sure how to overcome though is that through having to combine two arrays that have been generated through get_posts() (don’t think you can put query_posts() in to an array, but please correct me if I am wrong) I have all my posts displayed on a single page, rather that only 10 a page as I could through the post_per_page argument for query_posts(). Any ideas how to solve this at all?

    Thanks.

    esmi

    (@esmi)

    Forum Moderator

    An array_merge has worked for me thus far. Not 100% sure if that is THE correct way to do it though.

    Found another way around this now as could not get anywhere with what I originally wanted to do!

    Thanks for the advice.

    Nice one!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Combine two queries in to one’ is closed to new replies.