In a nutshell:

  • I’ve successfully added a custom field to posts on an existing WordPress site.
  • I’ve added that custom field to the columns available to admin users on the “All Posts” page of the dashboard.
  • Administrative users can sort the posts by the custom value.
  • When sorting by the custom value, only those posts that have been saved since I’ve added the custom field show up in the sorted values.

The following Post class is included in functions.php, and setup() is called within functions.php as well.

class Post {

public static function setup() {

    // Generated by Advanced Custom Fields:
    if(function_exists("register_field_group"))
    {
        register_field_group(array (
            'id' => 'acf_author-credit',
            'title' => 'Author Credit',
            'fields' => array (
                array (
                    'key' => 'field_591228ca41a8a',
                    'label' => 'Author Credit',
                    'name' => 'author_credit',
                    'type' => 'text',
                    'instructions' => 'Publishing content authored by someone else? Use this field to credit them!',
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'formatting' => 'none',
                    'maxlength' => '',
                ),
            ),
            'location' => array (
                array (
                    array (
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'post',
                        'order_no' => 0,
                        'group_no' => 0,
                    ),
                ),
            ),
            'options' => array (
                'position' => 'normal',
                'layout' => 'no_box',
                'hide_on_screen' => array (
                ),
            ),
            'menu_order' => 0,
        ));
    }

    add_action( 'manage_post_posts_custom_column' ,     array( 'Post', 'post_custom_columns' ), 10, 2 );
    add_action( 'pre_get_posts',                        array( 'Post', 'pre_get_post_posts' ) );
    add_filter( 'manage_edit-post_columns',             array( 'Post', 'edit_post_columns' ) ) ;
    add_filter( 'manage_edit-post_sortable_columns',    array( 'Post', 'edit_post_sortable_columns' ) ) ;

}


// Column Methods --------------------------------------------------------------------------------------------------

public static function edit_post_columns( $columns ) {

    $columns['author_credit'] = __( 'Author Credit', SOHE::SLUG );
    return $columns;

}

public static function pre_get_post_posts( $query ) {
    if ( is_admin() && $query->get( 'post_type' ) == 'post' ) {
        $orderby = $query->get( 'orderby' );
        if ( ! empty( $orderby ) ) {
            $meta_key = null;
            switch( $orderby ) {
                case 'author_credit':
                    $meta_key = $orderby;
                    break;
                default:
                    break;
            }
            if ( $meta_key != null ) {
                $query->set( 'meta_key', $meta_key );
                $query->set( 'orderby', 'meta_value' );
            }
        }
    }
}

public static function edit_post_sortable_columns( $columns ) {
    $columns['author_credit'] = 'author_credit';
    return $columns;
}

public static function post_custom_columns( $column, $post_id ) {

    switch( $column ) {
        case 'author_credit':
            $author_credit = get_post_meta( $post_id, 'author_credit', '' );
            if ( ! empty( $author_credit ) ) {
                echo $author_credit[0];
            }
            break;
        default:
            break;
    }

}

The WordPress site has hundreds of existing posts, and I’m loath to update all of them just to get this to work. Ideally, I’d like the pre_get_post_posts function defined above to sort by the meta_key for all posts, even those without the meta_key. Is there a way to modify the query to pull this off?

Read more here: Sorting posts by meta value that may not exist


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: