1

Note : In this question "Tag" means Tag or a Custom Taxonomy (Assume that it is topic).

It is hard to explain my question with words. So I will explain it using following example.

When user visit example.com/tag/google/ or example.com/topic/google/ should be out all posts which has 'google' word in title.

I am going to use pre_get_posts action to change the loop. But I have no any idea how to modify the loop. I could not find any thing in WP Query documentation also.

0

You can check if you are on a tag page with a conditional, and then modify the main query:

add_action( 'pre_get_posts', 'search_the_title', 1 );
function search_the_title($query){
    // Check if it's the main query
    if ( $query->is_main_query() && !is_admin()) {
        // If it's a tag page, search posts by tag
        if ( is_tag() ) { 
            $query->set('s' , get_query_var('tag' , ''));
            // Unset the current tag to search all posts
            unset( $query->query['tag'] );
            return;
        }
        // If it's a taxonomy page, search by taxonomy value
        if ( is_tax('topic') ){
            $query->set('s' , get_query_var('topic' , ''));
            // Unset the current taxonomy to search all posts
            unset( $query->query['tax_query'] );
            return;
        }
    }
}
|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.