0

I'm having trouble to exclude specific posts in wp_dropdown_pages. Exclude seems to work, but the first ID of my foreach is ever and ever include in the dropdown... I don't understand why...

My get_posts query

$exclude_posts = get_posts(array(
        'post_type' => 'organic-pickup',
        'tax_query' => array(
        array(
            'taxonomy' => 'type',
            'field' => 'term_id',
            'terms' => 72 //TODO Get all ID of taxonomy 'type' terms where types_metabox_dropdown = types_metabox_dropdown_no
                )
        )
        ));

My foreach with output formated to match wp_dropdown_pages parameters.

foreach($exclude_posts as $exclude_post) {
            $excludes[] = $exclude_post->ID;
        }
        $exclude = implode(',',$excludes);
        $excluded = "'" . $exclude . "'";
        print_r($excluded);

My dropdown

$ogpk_pickup_field_args = array (
            'depth' => -1,
            'class' => 'store-pickup form-row-wide',
            'show_option_none' => __( 'Choisissez le point relais où retirer votre panier', 'organic-pickup' ),
            'name' => 'store_pickup',
            'hide_empty' => false,
            // 'exclude' => '1074,1072,1059',
            'exclude' => $excluded, //TODO get excluded working please...
            'post_type' => 'organic-pickup',
            'value_field' => 'ID',
            'suppress_filters' => true
        );
        wp_dropdown_pages( $ogpk_pickup_field_args );

I commentend out an exclude line with '1047,1072,1059'. With this param, the dropdown works fine and don't dispaly this posts. The print_r($excluded) display this exact string... but put in the dropdown param, it always display the post corresponding to the first ID... I'm completly unable to understand why... Any hint folks?

2
  • don't add the single quotes. literals are enclosed in quotes to show it's a string, $exclude is already a string variable. Just pass $exclude, don't need $excluded at all.
    – inarilo
    Commented Jun 3, 2017 at 7:06
  • You are right inarilo! I've had so much trouble before finding implode to add comma to my output that I didn't test $exclude alone... You saved my day! Commented Jun 3, 2017 at 7:17

1 Answer 1

0

You don't need to add single quotes before using $exclude. Literals are enclosed in quotes to show it's a string, but since $exclude is already a string variable, you can pass it directly.

Remove $excluded, you don't need it at all and just use

$ogpk_pickup_field_args = array (
        'depth' => -1,
        'class' => 'store-pickup form-row-wide',
        'show_option_none' => __( 'Choisissez le point relais où retirer votre panier', 'organic-pickup' ),
        'name' => 'store_pickup',
        'hide_empty' => false,
        // 'exclude' => '1074,1072,1059',
        'exclude' => $exclude,
        'post_type' => 'organic-pickup',
        'value_field' => 'ID',
        'suppress_filters' => true
    );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.