The difference between add_filter and add_action

I looked through my functions. php and wondered why CODE A uses add_actionwhile CODE B uses add_filter?
The main goal of CODE A is to include and exclude categories .
The main purpose of CODE B is to exclude certain categories.

Is it correct to use add_actionfor CODE A
and add_filterfor CODE B?

.
CODE A: Display a specific category (called a "tag") for the main page, not "recent posts"

function featured_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'category_name', 'featured' );
    $query->set( 'category__not_in', array(60, 61) );
    $query->set( 'posts_per_page', 5 );
    }
}
add_action( 'pre_get_posts', 'featured_category' );

.
.
CODE B: Exclude β€œpost sponsored article categories” for search results

function search_filter ($ query) {

    if ( $query->is_search && $query->is_main_query() ) {
    $query->set('post_type', 'post');
    $query->set( 'category__not_in', array(60, 61) );
    $query->set( 'posts_per_page', 20 );
    }

    return $query;
}    
add_filter('pre_get_posts', 'search_filter');
+4
1

pre_get_posts - , . $query , CODE A , .

CODE B $query, , . .

do_action_ref_array( 'pre_get_posts', array( &$this ) ); 

add_action add_filter , (add_action add_filter). , - add_action.

+1

All Articles