Skip to content

Admin Filter, not filling in select value with array key #196

@thedavidthomas

Description

@thedavidthomas

Hello,

I have two post types product and product-collection
Product posts have a collection attached via an ACF Post Object field called collection

admin_filters in the admin the select field value="" is the same as the option label, I would expect the post ID to be used as the option value.

register_extended_post_type('product', [
        'menu_icon' => 'dashicons-products',
        'enter_title_here' => 'Name',
        'supports' => ['title', 'meta_key', 'page-attributes'],
        'hierarchical' => false,
        'dashboard_glance' => false,
        'quick_edit' => true,
        'has_archive'   => false,

        'admin_cols' => [
            'collection' => [
                'title'       => 'Collection',
                'meta_key' => 'collection',
                'function' => function(WP_Post $post) {
                    $collection = get_field('collection', $post->ID);
                    if(!empty($collection)) {
                        echo $collection->post_title;
                    }
                },
            ],

        ],
        # Add some dropdown filters to the admin screen:
        'admin_filters' => [
            'collection' => [
                'meta_key' => 'collection',
                'options'  => 'get_collection_values',
            ],
        ]
    ],
    [
        'singular' => 'Product',
        'plural'   => 'Products',
        'slug'     => 'product',
    ]
    );

    function get_collection_values() {
            $args = array(
                'post_type'=> 'product-collection',
                'orderby'    => 'ID',
                'post_status' => 'publish',
                'order'    => 'DESC',
                'posts_per_page' => -1
            );
            $result = new WP_Query( $args );

            $options = [];
            foreach($result->posts as $post) {
                $options[$post->ID] = $post->post_title;
            }

            print_r($options);
            return $options;
    }

This is what gets output in the admin

	Array
(
    [428] => Circa
    [144] => 3001
    [57] => Voda
    [56] => Suba
    [55] => Scala
    [54] => Monsoon
    [53] => Duet
    [51] => Calibre
)
<label for="filter_collection" class="screen-reader-text">Filter by Collection</label>				
<select name="collection" id="filter_collection">
        <option value="">All Collections</option>
        <option value="Circa">Circa</option>
	<option value="3001">3001</option>
	<option value="Voda">Voda</option>
	<option value="Suba">Suba</option>
	<option value="Scala">Scala</option>
	<option value="Monsoon">Monsoon</option>
	<option value="Duet">Duet</option>
	<option value="Calibre">Calibre</option>
</select>

I would expect output to be like

        <option value="">All Collections</option>
        <option value="428">Circa</option>
	<option value="144">3001</option>
	<option value="57">Voda</option>

I can confirm changing the URL manually is correctly filtering the results.

It works when I remove the options function and keep only meta_key but then only shows the ID not the post_title.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions