The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress Post Queries

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
  • #41951

    Someone in our design team has suggested a fairly tricky layout, and I’m hoping to get some help (snippet suggestions if possible) of how I would achieve this…

    I need to split my category archive (category.php / archive.php) in the following way:

    1. Display one featured post (tag = “featured”) at the top of the page
    2. Display two posts tagged as “hot”
    3. Display two popular posts
    4. Then display two latest posts (the ‘regular’ archive, which could include articles form the above, but only display 2)

    For displaying only 2 of the latest articles, I have the following down…

    < ? php $frontpage_latest_loop = new WP_Query( array( 'posts_per_page' => 2 ) ); ? >
    < ? php if ($frontpage_latest_loop->have_posts() ) : ?>
    < ? php while ( $frontpage_latest_loop->have_posts() ) : $frontpage_latest_loop->the_post(); ? >
    < !-- article goes here -->
    < ?php else : ?>
    < p >There are currently no articles to display. Please keep checking back.
    < ?php endif; ? >

    Then, I’m assuming that the following would work for showing articles from the ‘featured’ and ‘hot’ tags…

    < ?php $tag_featured_loop = new WP_Query( array( 'posts_per_page' => 5, ‘tag’ => ‘featured’ ) ); ? >

    I’m just a little unsure of whether or not this is possible in category.php in addition to the ‘regular’ posts loop. And, I’d like to know if I’m on the right track with my queries.

    Any help would be appreciated. Thanks in advance.

    Andy Howells

    Your best bet is to check out WP Query on the WordPress Codex –


    Hey, Andy

    I’ll admit to having accidentally pressed ‘post’ instead of ‘save draft’, so you might’ve had a reason for posting that rather unnecessary link in your reply. You might want to rethink that approach in future, as complete newcomers to the forum (who are probably already intimidated by having to ask pros for help) would be scared off…

    That said, I’ve often found when giving an elaborate explanation of my problem on any forum, the length determines whether or not you even get a reply. Thanks for the reply, albeit what it is.

    I’ve included the bits of code I forgot to paste. Again, any help would be appreciated.


    You should be able to run as many loops as you like using WP_Query – I’m not 100% sure what the problem is that you are experiencing. I’d say give it a go and if you stumble, we’ll be able to answer those questions!


    Hey, Doc

    It was more a question of how many query loops I could run… I’ll give it a go. Thank you.

    Andy Howells

    @jonedwards, when I originally posted there was no code above, just a question about how to do it.

    Regarding WP Query, you can run it as many times as needed, providing you close the loop before creating a new one.


    Okay, so I have come unstuck on this query thing and need some help, please?

    In my category.php file, I need to run a few queries (using WP_Query) in order to display the following:
    * One post from the the current category that is tagged as “featured”
    * Two posts from the current category that is tagged “hot”
    * Two posts from the current category

    So my questions are:
    1. Can you use QP_Query to find articles tagged with a particular tag, but only from a particular category? i.e. Category = DVD and Tag = Featured?
    2. Can you do the above for the currently viewed category? i.e. When browsing DVD, the category.php template is called, but I only want to show featured posts from that category…

    All I have down thus far is:

    < ?php $my_query = new WP_Query( array( 'category' => ‘my-category’ , ‘tag’ => ‘my-tag’, ‘posts_per_page’ => 1) ); ? >
    < ?php while ( $my_query->have_posts() ) : $my_query->the_post(); ? >
    < ?php endwhile; ? >

    But that selects either the category or the tag, not both.

    And then to get the current category, I tried:

    < ?php $current_category = single_cat_title("", false); ? >
    < ?php $my_query = new WP_Query( array( 'category' => ‘$current_category , ‘tag’ => ‘my-tag’, ‘posts_per_page’ => 1) ); ? >
    < ?php while ( $my_query->have_posts() ) : $my_query->the_post(); ? >
    < ?php endwhile; ? >

    Which is a bit of a hack at best…

    Any help would be appreciated. Thanks in advance.

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.