WordPress.org

Ready to get started?Download WordPress

Forums

Custom column, order by option (2 posts)

  1. Cultivate
    Member
    Posted 3 years ago #

    I am working with the events manager plugin on a site. I have managed to add an extra column to the posts admin page which displays the event start date.

    What i'd like to be able to do is order the posts by the event start date, in the same way you can order by date and title. Is this possible?

    Cheers

    Heres the code ive added to my functions file:

    function my_columns($columns) {
        $columns['event'] = 'Event Date';
        return $columns;
    }
    
    function my_show_columns($name) {
        global $post;
        switch ($name) {
            case 'event':
                $event = get_post_meta($post->ID, '_EventStartDate', true);
                echo $event;
        }
    }
    
    add_filter('manage_posts_columns', 'my_columns'); // post columns
    add_action('manage_posts_custom_column',  'my_show_columns'); // Add custom column
  2. liperuf
    Member
    Posted 3 years ago #

    dpc036, I had the same question a few days ago.

    It was not easy for me to find out how to make my custom columns sortable. This article helped me a lot: http://scribu.net/wordpress/custom-sortable-columns.html

    You'll need 3 hooks to do the job:

    1. manage_edit-{post-type}_columns
    2. manage_edit-{post-type}_sortable_columns
    3. manage_posts_custom_column

    While the first hook tells WP: "Ok, put this column here for me", the second one says: "Now, this custom column will append this query string".

    I my case, the third hook almost done nothing, because I created a column called "menu_order", a native attribute from WP (used in Pages context). So, read the post above to deal with your specific case.

    I hope it helps!

    Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic