WordPress.org

Ready to get started?Download WordPress

Forums

Display blog posts from specific category AND tag (13 posts)

  1. amplifiedshock
    Member
    Posted 4 years ago #

    I know it's easy to display blog posts from EITHER a category or tag. However, I'd like WordPress to display content that is posted in a specific category AND with a specific tag.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

  3. amplifiedshock
    Member
    Posted 4 years ago #

    Thank-you for this. I don't really know how to program so how do I combine the codes:

    Multiple category handling:
    query_posts(array('category__and' => array(2,6)));

    and

    Multiple tag handling:
    query_posts(array('tag__and' => array('bread','baking'));

    So that the above would return only posts in both categories 2 and 6, AND tagged with both bread and baking?

    Thanks!

  4. nims
    Member
    Posted 4 years ago #

    I am also looking for a similar solutioin.
    I need to have links in my sidebar like "Bread" that should take users to the archive page showing posts under category "baking" and tagged with "Bread".

    Is there anything like http://www.site_url.com/category/baking/tag/bread

  5. dikkevandale
    Member
    Posted 4 years ago #

    @nims

    I'm still looking for the same solution you were looking for. Linking to a URL just like you state in your example!

    I haven't figured it out yet :( Did you?!

  6. vtxyzzy
    Member
    Posted 4 years ago #

    @amplifiedshock try this:

    $args = array(
       'category__and' => array(2,6),
       'tag__and' => array('bread','baking')
    );
    query_posts($args);

    @nims & dikkevandale: make a template using the query you want and post a link to a page using that template. See the Codex Stepping Into Templates

  7. dikkevandale
    Member
    Posted 4 years ago #

    @vtxyzzy, I can do that easily :) No problem, it works.

    But I want to load it with variables, because the combinations are endless. That would let me make more than 100+ templates. If it could be solved with a variable enabled query, I don't have to.

  8. vtxyzzy
    Member
    Posted 4 years ago #

    @dikkevandale, are you OK with using Custom Fields to supply the variables? If so, you could use code like this (UNTESTED):

    <?php $my_cats = explode(',',get_post_meta($post->ID,'categories-to-show',TRUE));
    $my_tags = explode(',',get_post_meta($post->ID,'tags-to-show',TRUE));
    $args = array(
       'category__and' => $my_cats,
       'tag__and' => $my_tags
    );
    query_posts($args);

    where the Custom Field 'categories-to-show' is a comma separated list of category ids, and 'tags-to-show' is a comma separated list of tags.

    That way, one template would serve all pages.

  9. MichaelH
    Member
    Posted 4 years ago #

    Just a thought but I believe tag__and requires tag IDs.

  10. vtxyzzy
    Member
    Posted 4 years ago #

    @MichaelH, you are correct. Thanks for the info.

  11. nims
    Member
    Posted 4 years ago #

    So we are back to square one !

  12. Mark / t31os
    Moderator
    Posted 4 years ago #

    Not at all, is there a particular reason you can't use IDs in place of names?

  13. nims
    Member
    Posted 4 years ago #

    can someone help pls .... I hv still not found the answer

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags