• Hello, this is what I accomplish by now:
    – register custom post type “myevents”
    – create a custom field event-date (with plugin Advanced Custom Fields)
    – create a custom column (in admin page for myevents) to show event-date and make that column sortable
    Also, I have no problem displaying/showing events on page templates.

    The thing I want to change is the myevents order on admin page.
    So, on admin page for cpt myevents I can see the column with event dates ( also accomplish to show dates in my local format).
    And that column IS SORTABLE WHEN I CLICK on it.

    But when I enter that admin page for cpt myevents, all events are first sorted by the “published on” order – sorted by the date when they are published.

    I would like to sort them from the beginning, the very moment I open the admin page, by their custom field event-date which is visible in the custom column I made.

    For now I have to click on that column to sort them.

    All the articles and tutorials helped me to make myevents and sortable custom column, but I don’t know how to accomplish so events are sorted
    by event-date the first moment I open their admin page.

    Here is the code (register cpt):

    <?php
    /*
    Plugin Name: MyCPT
    */
    add_action( 'init', 'my_cpt' );
    
    function my_cpt() {
    
    register_post_type( 'myevents', array(
      'labels' => array(
        'name' => 'My events',
        'singular_name' => 'My event',
    	'add_new' => __('Dodaj Novu'),
    	'add_new_item' => __('Dodaj Novu Najavu'),
        'edit_item' => __('Uredi Najavu'),
        'new_item' => __('Nova Najava'),
        'view_item' => __('Pregled Najave'),
       ),
      'description' => 'Najave.',
      'public' => true,
      'menu_position' => 20,
      'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail', 'excerpt' ),
      'taxonomies' => array( 'category', 'post_tag' )
    ));
    }
    ?>

    And here is the code from my functions.php (for sortable custom column), my custom field for event-date is called “datum_najave”:

    <?php
    // Register a new column in the admin Post area
    
    add_filter('manage_edit-myevents_columns', 'my_dat_najave_column');
    function my_dat_najave_column($columns) {
        $columns['datumnajave'] =__('Datum najave');
        return $columns;
    }  
    
    // add content
    
    add_action( 'manage_myevents_posts_custom_column', 'my_dat_najave_column_content', 10, 2 );
    function my_dat_najave_column_content( $column_name, $post_id ) {
        if ( 'datumnajave' != $column_name )
            return;
        //Get dates from post meta
        $datumnajave = get_post_meta($post_id, 'datum_najave', true);
        esc_html_e( date_i18n( 'd.m.Y.' , strtotime( $datumnajave ) ) );
    }  
    
    //  make the column sortable
    
    add_filter( 'manage_edit-myevents_sortable_columns', 'my_sortable_datum_column' );
    function my_sortable_datum_column( $columns ) {
        $columns['datumnajave'] = 'datum';  
    
        //To make a column 'un-sortable' remove it from the array
        //unset($columns['date']);  
    
        return $columns;
    }  
    
    add_action( 'pre_get_posts', 'my_datum_orderby' );
    function my_datum_orderby( $query ) {
        if( ! is_admin() )
            return;  
    
        $orderby = $query->get( 'orderby');  
    
        if( 'datum' == $orderby ) {
            $query->set('meta_key','datum_najave');
            $query->set('orderby','meta_value_num');
        }
    }  ?>

    I don’t know what to add to make events sortable by default by “datum_najave” instead of their “published on” date.

  • The topic ‘Sort events on admin automatically by event-date’ is closed to new replies.