Support

Account

Home Forums General Issues search specific ACF field in admin/Posts

Solved

search specific ACF field in admin/Posts

  • Hi

    i have come across this code in order to search specific ACF field/fields in admin posts:

    https://wordpress.stackexchange.com/questions/16637/how-to-filter-post-listing-in-wp-dashboard-posts-listing-using-a-custom-field/16641#16641

    i have inserted the code into my function.php and works fine

    only problem with it that it is displaying all ACF fields in the drop down list which i want to display only some specific fields such as: source, source author, translated by and so on.

    can anyone help modify the code as per my needs?

  • Instead of doing the query to get a list of all avialble fields, construct an array with the field that you want to search.

    
    $fields = array(
      'field_1',
      'field_2',
      'field_3',
      'field_4',
      'etc...'
    );
    

    then alter the code used to display the field to use this new array

    
    foreach ($fields as $field) {
    	printf('<option value="%s"%s>%s</option>', $field, $field == $current? ' selected="selected"':'', $field);
    }
    
  • Hi John

    thanks for the reply.

    so my code bellow how it will be after adding the codes you have in your reply?

    could you please reply with the full code as per you early reply.

    
    
    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
    }
    
  • 
    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()
    {
    $fields = array(
      'field_1',
      'field_2',
      'field_3',
      'field_4',
      'etc...'
    );
    ?>
    <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) {
    	printf('<option value="%s"%s>%s</option>', $field, $field == $current? ' selected="selected"':'', $field);
    }
    ?>
    </select> <?php _e('Value:', 'baapf'); ?><input type="TEXT" name="ADMIN_FILTER_FIELD_VALUE" value="<?php echo $current_v; ?>" />
    <?php
    }
    
  • thanks a lot John, works perfect.

  • @hube2 can we implement the same in the users. I am using Ultimate membership plugin. so in that we get separate users tab which shows all the uses present. so i wan this feature to be used over there as well. can you help me out how and what will the changes that will take place in the above code.
    please i am stuck on this for a while now.
    Thank you

  • I don’t have the answer to that. I do not know if the same filters run on the user admin page.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.