WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Combine two queries in to one (11 posts)

  1. David Gard
    Member
    Posted 3 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    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'),?

  3. David Gard
    Member
    Posted 3 years ago #

    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!

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    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?

  5. David Gard
    Member
    Posted 3 years ago #

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

  6. esmi
    Forum Moderator
    Posted 3 years ago #

    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.

  7. David Gard
    Member
    Posted 3 years ago #

    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.

  8. esmi
    Forum Moderator
    Posted 3 years ago #

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

  9. David Gard
    Member
    Posted 3 years ago #

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

    Thanks for the advice.

  10. Taylor Baybutt
    Member
    Posted 3 years ago #

  11. David Gard
    Member
    Posted 3 years ago #

    Nice one!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.