WordPress.org

Ready to get started?Download WordPress

Forums

Listing posts belonging to two categories (wp_query) (3 posts)

  1. mikuniek
    Member
    Posted 4 years ago #

    Hi there!

    This is my first post, I hope that during work with wordpress I'll gain useful knowledge and I'll be able to share it with you :).

    My question concerns wp_query.

    I have few categories on my site, let's say it's:
    -articles (parent of below listed)
    --sport
    --lifestyle
    -featured

    And I have a post X that belongs to sport, articles and featured. On a specific place (using templates) I'd like to list posts belonging at same time to sport and featured but not those belonging to sport only. So if X belongs to SPORTS - it's not listed. If it belongs to SPORTS and FEATURED it is listed.

    Here's another catch: I'd like those posts to be displayed by category.php so I could detect what category is being displayed. So: if I'm browsing ARTICLES the query should list ARTICLES&FEATURED if in SPORT - SPORT&FEATURED.

    Here's my query:

    <?php $featured = new WP_Query('category_name=featured' ); ?>
    <?php while ($featured->have_posts()) : $featured->the_post(); ?>
    (...)
    I do not know hot to get current category here and how to link it with 'category_name=featured' in the query.

    Any suggestions will be appreciated.

  2. MichaelH
    Member
    Posted 4 years ago #

    Look at using the category__and arguments with your query.

    You will probably have to craft your own category display using Template_Tags/get_the_category.

    //to get current queried category
    if ( is_category() ) {
      $current_cat = get_query_var('cat');
    }
  3. mikuniek
    Member
    Posted 4 years ago #

    Ok, I'll try this, thank you! Will let you know if it works.

Topic Closed

This topic has been closed to new replies.

About this Topic