Search
 
SCRIPT & CODE EXAMPLE
 

PHP

How to filter custom posts by custom field in WordPress admin area

/** Create the filter dropdown */
add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
 
function wpse45436_admin_posts_filter_restrict_manage_posts(){
    $type = 'post';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
 
    //add filter to the post type you want
    if ('NAME_OF_YOUR_POST' == $type){ //Replace NAME_OF_YOUR_POST with the name of custom post
        $values = array(
            'label1' => 'value1', //Replace label1 with name and value1 with the value of custom field
            'label2' => 'value2', //Replace label2 with name and value2 with another value of custom field
        );
        ?>
        <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 } } /** if submitted filter by post meta */ add_filter( 'parse_query', 'wpse45436_posts_filter' ); function wpse45436_posts_filter( $query ){ global $pagenow; $type = 'post'; if (isset($_GET['post_type'])) { $type = $_GET['post_type']; } //Replace NAME_OF_YOUR_POST with the name of custom post if ( 'NAME_OF_YOUR_POST' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') { $query->query_vars['meta_key'] = 'META_KEY'; //Replace META_KEY to the actual meta key
        $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
    }
}
Comment

How to Filter Your Posts & Pages by Custom Field in WordPress Dashboard

add_filter( 'parse_query', 'ba_admin_posts_filter' );
add_action( 'restrict_manage_posts', 'ba_admin_posts_filter_restrict_manage_posts' );
 
function ba_admin_posts_filter( $query )
{
    global $pagenow;
    if ( is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_NAME']) && $_GET['ADMIN_FILTER_FIELD_NAME'] != '') {
        $query->query_vars['meta_key'] = $_GET['ADMIN_FILTER_FIELD_NAME'];
    if (isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '')
        $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
    }
}
 
function ba_admin_posts_filter_restrict_manage_posts()
{
    global $wpdb;
    $sql = 'SELECT DISTINCT meta_key FROM '.$wpdb->postmeta.' ORDER BY 1';
    $fields = $wpdb->get_results($sql, ARRAY_N);
?>
<select name="ADMIN_FILTER_FIELD_NAME">
<option value=""><?php _e('Filter By Custom Fields', 'baapf'); ?></option>
<?php
    $current = isset($_GET['ADMIN_FILTER_FIELD_NAME'])? $_GET['ADMIN_FILTER_FIELD_NAME']:'';
    $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
    foreach ($fields as $field) {
        if (substr($field[0],0,1) != "_"){
        printf
            (
                '<option value="%s"%s>%s</option>',
                $field[0],
                $field[0] == $current? ' selected="selected"':'',
                $field[0]
            );
        }
    }
?>
</select> <?php _e('Value:', 'baapf'); ?><input type="TEXT" name="ADMIN_FILTER_FIELD_VALUE" value="<?php echo $current_v; ?>" />
<?php
}
Comment

PREVIOUS NEXT
Code Example
Php :: http://www.endmemo.com/program/R/vector.php 
Php :: $_FILES image dimensions 
Php :: laravel schedule run 
Php :: PHP: how to "clone from" another object of same class 
Php :: Laravel API ResourceCollection doesnt works 
Php :: codeception field datetime firefox 
Php :: vscode php debugger change value 
Php :: waht is middleware in laravel 
Php :: vault create/enable secret engine 
Php :: add image thumb on checkout woo 
Php :: carbon get difference between two dates in years and months 
Php :: laravel gigapay payout list 
Php :: $query-free_result(); 
Php :: afosto/yaac error parsing certificate request 
Php :: function to fetch user details 
Php :: old codestar gallery 
Php :: Yii2: Setting default values for all attributes of a model 
Php :: post with count greater than 1 laravel 
Php :: Save custom input field value into cart item 
Php :: trim string in php codeigniter 
Php :: Laravel Auth successfully logged in but keep redirecting to login page and not showing failed error flash message 
Php :: laravel validateexception no error description 
Php :: laravel count 
Php :: Allow mass assignment in Laravel 
Php :: herencia php 
Php :: php cors error 
Php :: how to validate students who made payment in php and mysql 
Php :: php eval base64_decode 
Php :: code to set error for du[licate entry in php 
Php :: fetch email from url contact form 7 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =