Skip to content

Instantly share code, notes, and snippets.

@mirkoschubert
Last active January 27, 2022 16:49
Show Gist options
  • Save mirkoschubert/b75b329a03be50f6b0cdd95ae77c6761 to your computer and use it in GitHub Desktop.
Save mirkoschubert/b75b329a03be50f6b0cdd95ae77c6761 to your computer and use it in GitHub Desktop.
WordPress Custom Post Types Events with specific parent page
<?php
define('EVENTS_PARENT_ID', 885);
function event_category_taxonomy() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name', 'domain' ),
'singular_name' => _x( 'Category', 'taxonomy singular name', 'domain' ),
'search_items' => __( 'Search Categories', 'domain' ),
'all_items' => __( 'All Categories', 'domain' ),
'parent_item' => __( 'Parent Category', 'domain' ),
'parent_item_colon' => __( 'Parent Category:', 'domain' ),
'edit_item' => __( 'Edit Category', 'domain' ),
'update_item' => __( 'Update Category', 'domain' ),
'add_new_item' => __( 'Add New Category', 'domain' ),
'new_item_name' => __( 'New Category Name', 'domain' ),
'menu_name' => __( 'Category', 'domain' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'query_var' => true,
'rewrite' => true,
);
register_taxonomy( 'event_category', [ 'event' ], $args );
}
add_action('init', 'event_category_taxonomy');
function event_post_type() {
$labels = array(
'name' => _x( 'Events', 'Post Type General Name', 'domain' ),
'singular_name' => _x( 'Event', 'Post Type Singular Name', 'domain' ),
'menu_name' => __( 'Events', 'domain' ),
'parent_item_colon' => __( 'Parent Event', 'domain' ),
'all_items' => __( 'All Events', 'domain' ),
'view_item' => __( 'View Event', 'domain' ),
'add_new_item' => __( 'Add New Event', 'domain' ),
'add_new' => __( 'Add New', 'domain' ),
'edit_item' => __( 'Edit Event', 'domain' ),
'update_item' => __( 'Update Event', 'domain' ),
'search_items' => __( 'Search Event', 'domain' ),
'not_found' => __( 'Not Found', 'domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'domain' ),
);
$args = array(
'label' => __( 'events', 'domain' ),
'description' => __( 'Event', 'domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields' ),
'taxonomies' => array( 'event_category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-calendar-alt',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'rewrite' => array(
'slug' => 'aktuelles/veranstaltungen',
'with_front' => false,
),
);
register_post_type( 'event', $args );
}
add_action( 'init', 'event_post_type', 0 );
function add_event_parent($data, $postarr, $unsanitized_postarr) {
global $post;
if ($post->post_type == "event") {
$data["post_parent"] = EVENTS_PARENT_ID;
}
return $data;
}
add_filter('wp_insert_post_data', 'add_event_parent', 99, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment