someone can help me to understand what exactly do this function declared into the functions.php file of a theme?

function exclude_featured_tag( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'tag__not_in', 'array(ID OF THE FEATURED TAG)' );
add_action( 'pre_get_posts', 'exclude_featured_tag' );

I am not into PHP but looking it I think that if the user is on the home page it modifya query object exluding the posts having a specific tag. Is it right?

If it's right can you explain me the last line:

add_action( 'pre_get_posts', 'exclude_featured_tag' );

I think that it is used to automatically call the previous function what a specific event happen...is it right? what event? can you help me to understand how exactly work?


  • Link to the source of the code please. Jul 26, 2014 at 21:43

1 Answer 1


The last line tells WP to run this function on the pre_get_posts hook. That hook is the ideal place to modify the main query that will be run in order to build the page. In this case, exclude_featured_tag() checks whether WP is loading the home page, and whether it's about to call the main query for the page (as opposed to any other secondary queries being run). Then it modifies the query ($query->set()) to NOT include any post that has tag of ID OF THE FEATURED TAG.

For more on the pre_get_posts hook, see the codex.

  • Perfect explaination. The only last doubt is about: who say that a query is the main query? Jul 27, 2014 at 9:09
  • 1
    When WP is setting up a page, there is always a main query. But there can be other, secondary queries that run. A common case is that a theme template, widget, plugin or what-have-you will call a second query via $second_query = new WP_query( $args ); in order to cherry pick a certain set of posts for special treatment of some sort. Checking for whether you're querying the main query assures that you don't interfere with any of those other things that may be happening on the page.
    – Caspar
    Jul 27, 2014 at 12:36
  • Ok, perfect...now all si clear :-) Jul 27, 2014 at 14:12

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.