• Resolved dfwgreg

    (@dfwgreg)


    Hi everyone,
    Image of the edit.php

    Is there a way for the edit.php page to check if a custom field has been enabled? I have a custom field called ‘featured post’ and would like it to replace stickies as they don’t work on custom post types. I have a function enabled to check for a taxonomy and would to know if there’s one for custom field.

    Thanks,
    Gregory S.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter dfwgreg

    (@dfwgreg)

    UPDATE: I was able to find a solution but it displays all the custom fields. I only want it to display one custom field. I’m not familiar with PHP, so all help is greatly appreciated.

    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
    }
    ?>
    Thread Starter dfwgreg

    (@dfwgreg)

    Oh well, I can leave it like the above.

    Hi There,

    I came across your post and was using the same code. I needed to display a drop down with only one custom key.

    To do that I replaced

    $sql = 'SELECT DISTINCT meta_key FROM '.$wpdb->postmeta.' ORDER BY 1';
    to
    $sql= "SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE meta_key = 'product_code_box_text' ORDER BY 1";

    Hope that helps even now:)

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Post edit.php – add custom field to filter section’ is closed to new replies.