WordPress Custom Post Order and Category

I have a few issues I am running into. I have created a custom post in wordpress called Events. I have a way to add a new one and view all events as well as category. When I view all custom post pages in admin dashboard they are not in order by date. They are all over the place. When creating a new custom post page, I only want to choose specific categories. Not all the categories. Here is my code below. How do I get the categories to be specific categories only. And the order of viewing all custom posts most recent in dashboard. //Custom Post Types add_action('init', 'custom_post'); function custom_post() { register_post_type('events', array( 'label' => 'Events', 'singular_label' => 'Events', 'hierarchical' => true, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array('slug' => 'events'), 'capability_type' => 'post', 'taxonomies' => array( 'post_tag', 'category'), 'menu_position' => 5, 'menu_icon' => 'dashicons-calendar', 'supports' => array( 'title', 'editor', 'custom-fields', 'excerpt', 'thumbnail', 'page-attributes' ), ) ); } Thank you.

Comment (1)

Jese Leos

August 10, 2024

Verified user

For ordering custom posts by date you need to modify the query used to display the posts in dashboard and for displaying only specific categories you should modify the categories metabox to display them. This code is for ordering posts by date, put it in functions.php: // Order Events by date in admin dashboard add_action('pre_get_posts', 'set_events_order_in_admin'); function set_events_order_in_admin($query) { if (!is_admin()) { return; } global $pagenow; $post_type = $query->get('post_type'); if ($post_type == 'events' && $pagenow == 'edit.php') { $query->set('orderby', 'date'); $query->set('order', 'DESC'); } } and for limiting the categories displayed in category metabox for your custom post type, you can use 'register_taxonomy_for_object_type'. first you'll need to register a custom taxonomy specific to your 'events' post type. Again put this code in functions.php: // Register custom taxonomy for Events function create_event_taxonomies() { register_taxonomy( 'event_category', 'events', array( 'label' => __('Event Categories'), 'rewrite' => array('slug' => 'event-category'), 'hierarchical' => true, ) ); } add_action('init', 'create_event_taxonomies'); // Display only specific categories for Events function restrict_categories_for_events($post_type, $which) { if ($post_type !== 'events') { return; } $taxonomy = 'event_category'; $terms = get_terms(array( 'taxonomy' => $taxonomy, 'hide_empty' => false, )); if (!empty($terms) && !is_wp_error($terms)) { echo '<div class="categorydiv">'; echo '<ul class="category-tabs">'; echo '<li class="tabs"><a href="#' . $taxonomy . 'checklist">' . __('Event Categories') . '</a></li>'; echo '</ul>'; echo '<div id="' . $taxonomy . 'checklist" class="tabs-panel">'; echo '<ul id="' . $taxonomy . 'checklist" class="categorychecklist form-no-clear">'; foreach ($terms as $term) { $id = 'event_category-' . $term->term_id; echo '<li id="' . $id . '"><label class="selectit">'; echo '<input value="' . $term->term_id . '" type="checkbox" name="tax_input[' . $taxonomy . '][]" id="in-' . $id . '"> ' . $term->name . '</label></li>'; } echo '</ul>'; echo '</div>'; echo '</div>'; } } add_action('restrict_manage_posts', 'restrict_categories_for_events', 10, 2); Make sure you have categories created under the event_category taxonomy. You can manage these categories from the WordPress admin under the 'Events' custom post type.

You’ll be in good company