I am trying to have a page (tag page) with this conditions:

  • query posts from a specific category (for example : products)
  • show only the posts with a selected tag (the tag is variable) not all the posts from that category.
  • You're using the tag archive index page? Do you always want to limit the query to the specified category, or only under certain conditions? Also, what have you tried? – Chip Bennett Mar 11 '14 at 17:57
  • @ChipBennett yes. i have tag , archive and index. i have a page with a list of some tags. i need when a user click on the tag it shows the list of all posts with clicked tag. – user48792 Mar 11 '14 at 18:22

Since you're working with the tag archive index page query, this really won't be all that difficult. You simply need to filter the query parameters at pre_get_posts, using your criteria.

For example:

function wpse137627_pre_get_posts( $query ) {
    // If this is the tag archive index,
    // and is the main query
    if ( is_tag() && $query->is_main_query() ) {
        // Limit the query to the specified category
        // Change '123' (int) to the appropriate category ID
        $query->set( 'cat', 123 );
add_action( 'pre_get_posts', 'wpse137627_pre_get_posts' );

You can also pass the category slug, using $query->set( 'category_name', 'cat-slug' ).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.