Skip to:
Content
Pages
Categories
Search
Top
Bottom

wp_query filter topics by tag


  • cookiebear
    Member

    @cookiebear

    I am working on a wordpress template page.

    i am trying to retrieve a list of topic threads and filtering by tag.

    I can get topics without tag filtering by using something like this

    $forumargs = array(    'post_type' => 'topic', 'numberposts' => 15 );
    $mytopics = new WP_Query( $forumargs );

    while ( $mytopics->have_posts() ) : $mytopics->the_post();
    the_title();
    echo '<div class="entry-content">';
    the_content();
    echo '</div>';
    endwhile;

    but when i try to filter by tag , it returns nothing.

    $forumargs = array( 'tag'=>'mytagname', 'post_type' => 'topic', 'numberposts' => 15 );

    i have seen that there is something called tax_query, but i cannot figure out if i can use that, or how to use it.

    how should this work?

Viewing 3 replies - 1 through 3 (of 3 total)

  • Jared Atchison
    Moderator

    @jaredatch

    You are close. bbPress does not use the tag taxonomy, that is strictly for posts.

    You’ll need to use tax_query as you mentioned. This is untested but should give you a general idea :)

    $mytopics_args = array(
    'post_type' => 'topic',
    'numberposts' => 15,
    'tax_query' => array(
    array(
    'taxonomy' => bbp_get_topic_tag_tax_id(),
    'field' => 'slug',
    'terms' => 'you-tag-slug'
    )
    )
    );
    $mytopics = new WP_Query( $mytopics_args );


    cookiebear
    Member

    @cookiebear

    thanks jaredatch, that worked perfectly.

    i would have never got that taxonomy setting “bbp_get_topic_tag_tax_id() “


    Jared Atchison
    Moderator

    @jaredatch

    Great, you had most of it figured out though.

    That’s totally understandable – I couldn’t remember it off the top of my head either and had to go dig through some code – but that’s the best way to learn :)

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.