0

Assume that I have created plugin to include custom post type called site and plugin to search result. Assume I have added only following code to my plugin.

function searchAll( $query ) {
 if ( $query->is_search ) { $query->set( 'post_type', array( 'site', 'plugin' )); } 
 return $query;
}
add_filter( 'the_search_query', 'searchAll' );

When this function call? Is it call in every page views? Or is it call in only when some one search?

1 Answer 1

1

You've hooked into a filter. The code will run when that filter is executed. In your case, that is when the_search_query() function runs:

2595    /**
2596     * Display the contents of the search query variable.
2597     *
2598     * The search query string is passed through {@link esc_attr()}
2599     * to ensure that it is safe for placing in an html attribute.
2600     *
2601     * @since 2.1.0
2602     */
2603    function the_search_query() {
2604            /**
2605             * Filter the contents of the search query variable for display.
2606             *
2607             * @since 2.3.0
2608             *
2609             * @param mixed $search Contents of the search query variable.
2610             */
2611            echo esc_attr( apply_filters( 'the_search_query', get_search_query( false ) ) );
2612    }

WordPress core only runs that function a few times (you can grep your source to find it) but themes and plugins may run it as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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