December 30, 2024
ACF
1 min read
custom field filter wordpress

Show filter on custom post type admin area with custom field value

functions.php
<?PHP 
//admin filter
add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
/**
 * First create the dropdown
/** Create the filter dropdown */
function wpse45436_admin_posts_filter_restrict_manage_posts(){
    $type = 'custom_post_type'; // change to custom post name.
    if (isset($_GET['custom_post_type'])) {
        $type = $_GET['custom_post_type'];
    }

    //only add filter to post type you want
    if ('movies' == $type){
        //change this to the list of values you want to show
        //in 'label' => 'value' format
        $values = array(
            'label' => 'value1', 
            'label1' => 'value2',
            'label2' => 'value3',
        );
        ?>
        <select name="ADMIN_FILTER_FIELD_VALUE">
        <option value=""><?php _e('Filter By ', 'wose45436'); ?></option>
        <?php
            $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
            foreach ($values as $label => $value) {
                printf
                    (
                        '<option value="%s"%s>%s</option>',
                        $value,
                        $value == $current_v? ' selected="selected"':'',
                        $label
                    );
                }
        ?>
        </select>
        <?php
    }
}


add_filter( 'parse_query', 'wpse45436_posts_filter' );
/**
 * if submitted filter by post meta

 * @return Void
 */
function wpse45436_posts_filter( $query ){
    global $pagenow;
    $type = 'movies'; // change to custom post name.
    if (isset($_GET['custom_post_type'])) {
        $type = $_GET['custom_post_type'];
    }
    if ( 'movies' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
        $query->query_vars['meta_key'] = 'META_KEY'; // change to meta key created by acf.
        $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE']; 
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

WooCommerce Ajax Product Search and Category Filter Without Plugin

WooCommerce Ajax Product Search with Category Filter. Simple code, without any plugin.

Add an additional custom checkbox in the WooCommerce checkout

Add an additional custom checkbox after the terms and conditions in WooCommerce checkout we can use WooCommerce

Create a new WordPress administrator via functions.php & FTP

Sometimes, you might need to create an administrator account in WordPress without being able to access the admin dashboard. This could be because you have lost access to your site’s admin panel or when troubleshooting a client’s website. In this tutorial, we will show you how to programmatically add a WordPress administrator account using the […]

JS set interval for an event until element show

Sometimes we need to active an event when a specific element loads on-page or part of an element change.

Web Development Project in mind?

if you looking for a web developer for paid contribution to your project I am available for work.

Mukto
Mukto

Click the button below to chat with me.