Support

Account

Home Forums General Issues Exclude posts

Solving

Exclude posts

  • Hi,

    I need to query post ‘maintenance’ and exclude post with specific values ACF taxo ‘reparation_satut’==561

    I read i have to use pre_get_post but as my query is in another function, i don’t understand where and when pre_get_post will be applied.

    I would like to apply this filter only on my query not eveywhere on the site.

    thanks

    function exclude_repaired( $query ) {

    $custom_meta = array(

    ‘key’ => ‘reparation_statut’,

    ‘value’ => ‘561’,

    ‘compare’ => ‘!=’

    );

    }

    add_action( ‘pre_get_posts’, ‘exclude_repaired’ );

    function display_form_maintenance(){

    $args = array( /// here i don’t want posts with reparation_statut ID ==561

    ‘post_type’=> ‘maintenance’,

    ‘posts_per_page’ => -1,

    );

    echo ‘<select>’;

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {

    while ( $query->have_posts() ) {

    $query->the_post();

    echo ‘<option value=”‘.get_the_ID().'”>’.get_the_title().'</option>’;

    $post_id = get_the_ID();

    }

    }

    echo ‘</select>’;

    wp_reset_postdata();

    }

  • Old post I know but it popped to the top due to spammers.

    If anyone is looking or this you can add a filter that will run on just a specific query by adding the filter before your query and then removing it after.

    
    add_filter('pre_get_posts', 'my_pre_get_posts');
    // do your query
    remove_filter('pre_get_posts', 'my_pre_get_posts');
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.