1

I have to exclude the same terms from a custom taxonomy from queries in different places / php pages - in the main query (functions.php) and in two custom widgets (other two separated php files). How can I control the excluded terms list from only one plase?

The code for the main query:

// Add facebook_events post type to the main query
function add_custom_post_type_to_query( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'facebook_events') );
        $query->set( 'tax_query', array( 'category', 'facebook_category',
            array( //EXCLUDE TERMS FROM QUERY
                'taxonomy' => 'facebook_category',
                'terms' => array('term1', 'term2'), //EXCLUDE THESE TERMS FROM QUERY
                'field' => 'slug',
                'operator' => 'NOT IN',
            ),
        ) );
    }
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

The code for a custom widget:

$r = new WP_Query( apply_filters( 'widget_posts_args', array(
    'post_type'           => array( 'facebook_events' ),
    'orderby'             => 'meta_value', // We want to organize the events by date
    'order'               => 'ASC', // ASC is the other option
    'posts_per_page'      => $number,
    'meta_query'          => array( // WordPress has all the results, now, return only the events after today's date
        array(
            'key'         => 'event_start_date', // Check the start date field
            'value'       => date_i18n("Y-m-d"), // Set today's date (note the similar format)
            'compare'     => '<=', // Return the ones smaller than or equal to today's date
            'type'        => 'DATE' // Let WordPress know we're working with date
        ),
        array(
            'key'         => 'event_end_date', // Check the start date field
            'value'       => date_i18n("Y-m-d"), // Set today's date (note the similar format)
            'compare'     => '>=', // Return the ones greater than or equal to today's date
            'type'        => 'DATE' // Let WordPress know we're working with date
        )
    ),
    'tax_query'           => array(
        array( //EXCLUDE TERMS FROM QUERY
           'taxonomy' => 'facebook_category',
           'terms' => array( 'term1', 'term2' ), //EXCLUDE THESE TERMS FROM QUERY
           'field' => 'slug',
           'operator' => 'NOT IN'
        ),
    )
) ) );

UPDATE

I found a simple understandable solution by @Caspar here and a more sophisticated by @fuxia here, but the second is above my knowledge level - I need an example to understand how to use it in my situation, so, probably, I will use the first.

  • 1
    How about using a pre_get_posts filter? You can place your filter in functions.php and just specify which queries you want to affect, to exclude that taxonomy. codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts – WebElaine Sep 27 '17 at 15:43
  • @WebElaine How to control the main query with the pre_get_posts filter from function.php I know/understand (see my first code snippet), but how to control from the same code a query in a custom widget (see the second snippet) I don't. – Iurie Malai Sep 29 '17 at 5:54
  • I found a simple understandable solution here and a more sophisticated by @fuxia here, but the second is above my knowledge level - I need an example to understand how to use it in my situation, so, probably, I will use the first. – Iurie Malai Sep 29 '17 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.