Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create a extra column for year in my timeline custom post. I can create the extra column and I can actually get it sorting and it works correctly but then all my pages then dont seem to work.

this is all of my code i am using to create the column and make it sortable:

// Register the column
add_filter( 'manage_edit-maryg_timeline_columns', 'set_custom_edit_date_columns' );
add_action( 'manage_maryg_timeline_posts_custom_column' , 'custom_date_column', 10, 2 );

function set_custom_edit_date_columns($columns) {
    unset( $columns['date'] );
    $columns['timeline_date'] = __( 'Year', 'your_text_domain' );

    return $columns;
}

//output data for the column
function custom_date_column( $column, $post_id ) {
    switch ( $column ) {
        case 'timeline_date' :
            $year = get_post_meta( $post_id, 'timeline_date', true );

            if ( is_string( $year ) )
                echo $year;
            else
                echo 'Unable to retrieve year';
            break;
    }
}

//make columns sortable
add_filter("manage_edit-maryg_timeline_sortable_columns", 'timeline_sort');
function timeline_sort($columns) {
    // $custom = array(
    //     'timeline_date'   => 'Year'
    // );
    // return wp_parse_args($custom, $columns);
    //or this way
    $columns['timeline_date'] = 'Year';
    return $columns;
}

//make the date column sorted by year by default
add_filter( 'request', 'timeline_column_orderby' );
function timeline_column_orderby( $vars ) {
    if ( !isset( $vars['orderby'] ) || ( isset( $vars['orderby'] ) && 'Year' == $vars['orderby'] ) ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'timeline_date',
            'orderby' => 'meta_value'
        ) );
    }
    return $vars;
}

the problem code i have narrowed it down to is:

add_filter( 'request', 'timeline_column_orderby' );
function timeline_column_orderby( $vars ) {
    if ( !isset( $vars['orderby'] ) || ( isset( $vars['orderby'] ) && 'Year' == $vars['orderby'] ) ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'timeline_date',
            'orderby' => 'meta_value'
        ) );
    }
    return $vars;
}

The only thing is i dont understand why my pages not work any more

Thanks in advance

EDIT

I've narrowed it down even further to it being this line of code:

!isset( $vars['orderby'] ) ||

but what im trying to do is sort by this column automatically and if i remove that it doesnt do that

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

i fixed this issue by changing some of my code i have.

i changed:

add_filter( 'request', 'timeline_column_orderby' );

function timeline_column_orderby( $vars ) { if ( !isset( $vars['orderby'] ) || ( isset( $vars['orderby'] ) && 'Year' == $vars['orderby'] ) ) { $vars = array_merge( $vars, array( 'meta_key' => 'timeline_date', 'orderby' => 'meta_value' ) ); } return $vars; }

to:

add_action( 'load-edit.php', 'sort_year_load' );

function sort_year_load() {
    add_filter( 'request', 'sort_year' );
}

//make the date column sortable by year
function sort_year( $vars ) {

    /* Check if we're viewing the 'maryg_timeline' post type. */
    if ( isset( $vars['post_type'] ) && 'maryg_timeline' == $vars['post_type'] ) {

        /* Check if 'orderby' is set to 'year'. */
        if ( isset( $vars['orderby'] ) && 'Year' == $vars['orderby'] ) {

            /* Merge the query vars with our custom variables. */
            $vars = array_merge(
                $vars,
                array(
                    'meta_key' => 'timeline_date',
                    'orderby' => 'meta_value_num'
                )
            );
        }
    }

    return $vars;
}

which told wordpress how to sort the table when i click on the clickable table header.

what i am doing is only run my customization on the 'edit.php' page in the admin.

i then check if we're viewing the correct custom post and also if it is set to year. then i just order it by the meta_key.

my main issue i had was ordering the table on load. I fixed that with this code:

function set_year_post_type_admin_order($wp_query) {
    if (is_admin()) {
        $post_type = $wp_query->query['post_type'];

        if ( $post_type == 'maryg_timeline') {
            $wp_query->set('meta_key', 'timeline_date');
            $wp_query->set('orderby', 'meta_value_num');
            $wp_query->set('order', 'DESC');
        }
    }
}

add_filter ( 'pre_get_posts', 'set_year_post_type_admin_order' );

which checks if your an admin and the post type and then sets the query to the values i set.

Hope this might help someone else if they come across it

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.