WordPress.org

Forums

Displaying posts assigned to ANY 2+ categories (6 posts)

  1. wideaperture
    Member
    Posted 4 years ago #

    I'm redesigning my blog and I'd like to display a list (or just the latest) post that is assigned to multiple categories.

    I know about the category__and function, but so far as I can tell it requires you to specify which categories your post must belong to (e.g., show posts in categories 2 and 6).

    What'd I'd like to do is show any posts that belong to more than one category, but I can't find anything on how to do this in the codex or the forums.

    Is there a way to use category__and to ask for a list of all posts assigned to multiple categories?

    Is there another function that would do this instead?

    Alternatively, what might be the best way to go about writing a hack to do this?

  2. wideaperture
    Member
    Posted 4 years ago #

    p.s. I should mention that I'd like this list to go on the homepage, not a category archive - though a way to do that would be interesting, too.

  3. vean
    Member
    Posted 3 years ago #

    Hy,

    same problem here, only I'm looking for solution to show those posts in some category.

    Actually, let say,
    In category 5 I want to show post which belong to category 3 and 5.
    Or I can say...
    show post which belong to CURRENT CATEGORY and CATEGORY 5.

    Thanks for any help in advance.
    Andrija

  4. vean
    Member
    Posted 3 years ago #

    Anyone?

    In fact, is it possible to do something like that?

    Please......

  5. monkeymynd
    Member
    Posted 3 years ago #

    See here for full text: http://codex.wordpress.org/Template_Tags/query_posts

    Category Parameters

    Show posts only belonging to certain categories.

    * cat
    * category_name
    * category__and
    * category__in
    * category__not_in

    Show One Category by ID

    Display posts from only one category ID (and any children of that category):

    query_posts('cat=4');

    Show One Category by Name

    Display posts from only one category by name:

    query_posts('category_name=Staff Home');

    Show Several Categories by ID

    Display posts from several specific category IDs:

    query_posts('cat=2,6,17,38');

    Exclude Posts Belonging to Only One Category

    Show all posts except those from a category by prefixing its ID with a '-' (minus) sign.

    query_posts('cat=-3');

    This excludes any post that belongs to category 3.

    Multiple Category Handling

    Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6:

    query_posts(array('category__and' => array(2,6)));

    To display posts from either category 2 OR 6, you could use cat as mentioned above, or by using category__in (note this does not show posts from any children of these categories):

    query_posts(array('category__in' => array(2,6)));

    You can also exclude multiple categories this way:

    query_posts(array('category__not_in' => array(2,6)));

  6. vean
    Member
    Posted 3 years ago #

    Thank You VERY VERY MUCH monkeymynd!
    I have looked before that url You suggest me but then I didn't find answer. Now I did!!! :) Thanks once again.

    My questions was,
    How to show 1 post which belong to both:
    CURRENT Category + some other category?

    SOLUTION :
    =============================================================
    1. QUERY in category.php show 1 post which belong to both:
    current category + category 12 (ID).
    =============================================================
    <?php query_posts(array('showposts' => 1, 'category__and' => array($cat,12))); ?>

    <?php while (have_posts()) : the_post(); ?>

    <div class="post">
    bla bla bla ... define how is that post shown.
    </div>

    <?php endwhile; ?>

    =============================================================
    And below that query I have another query->
    =============================================================
    2. QUERY in category.php show all other posts which belong
    to current category as usually.
    =============================================================

    <?php
    $categoryvariable=$cat;
    $queryCAT= 'cat=' . $categoryvariable. '&orderby=date&order=ASC';
    query_posts($queryCAT); ?>
    <?php while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue;
    update_post_caches($posts); ?>

    <div class="post">
    bla bla bla ... define how is that post shown.
    </div>

    <?php endwhile; ?>
    =============================================================

    enjoy ;)

    P.S. sorry on my bad English

Topic Closed

This topic has been closed to new replies.

About this Topic