Powered by Blogger.

WordPress RSS feed – filter RSS content by custom field value


Recently my client added a new field to jobs with plugin Advanced Custom Fields. And he asked me to filter the sites’ feed to hide jobs with that field set to true.

After the search in Google and did some tests I use the following codes to achieve the goal.


function waytowp_filter_feed( $query ) {
    // only for feeds
    if( $query->is_feed && 
        $query->is_main_query() && 
        $query->query['post_type'] == 'jobs' && 
    $query->is_post_type_archive) {

        $query->set( 'meta_key', 'community_job' ); //community_job is field name that set in ACF
        $query->set( 'meta_value', 0 );
    } 
}

add_action( 'pre_get_posts', 'waytowp_filter_feed' );


No comments