Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thedavidthomas opened this issue Apr 13, 2022 · 1 comment
Closed

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

thedavidthomas opened this issue Apr 13, 2022 · 1 comment

Comments

@thedavidthomas
Copy link

thedavidthomas commented Apr 13, 2022

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.

@thedavidthomas
Copy link
Author

Just found Issue #87 and PR #107, with what sounds like same issue, sorry for opening a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant