Skip to content

Instantly share code, notes, and snippets.

@strsar
Created October 7, 2021 01:34
Show Gist options
  • Save strsar/be50b7be8e3465075d33d74bba352b5c to your computer and use it in GitHub Desktop.
Save strsar/be50b7be8e3465075d33d74bba352b5c to your computer and use it in GitHub Desktop.
[WP] Random custom post type functions
<?php defined('ABSPATH') or header('Location: /');
/**
* Register cpt post type
* `cpt`
*/
function cpt_init() {
$name = 'cpt';
$type = sanitize_title($name);
$labels = array(
'name' => __($name.'s'),
'singular_name' => __($name),
'menu_name' => __($name.'s'),
'all_items' => __('All '.$name.'s'),
'add_new' => __('Add '.$name),
'add_new_item' => __('Add New '.$name),
'edit_item' => __('Edit '.$name),
'new_item' => __('New '.$name),
'view_item' => __('View '.$name),
'search_items' => __('Search '.$name.'s'),
'not_found' => __('No '.$name.'s Found'),
'not_found_in_trash' => __('No '.$name.'s Found in the Trash'),
'featured_image' => __('Featured Image'),
);
$args = array(
'labels' => $labels,
'label' => __($name.'s'),
'description' => __($name.'s'),
'menu_icon' => 'dashicons-rest-api',
'capability_type' => 'page',
'public' => true,
'show_ui' => true,
'show_in_rest' => true,
'has_archive' => true,
'show_in_menu' => true,
'exclude_from_search' => false,
'map_meta_cap' => true,
'hierarchical' => true,
'query_var' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions'),
'rewrite' => array(
'with_front' => false,
'pages' => true,
'slug' => 'cpts'
),
);
register_post_type($type, $args);
}
add_action('init', 'cpt_init');
// Change archive order
add_action('pre_get_posts', function($query){
if(is_post_type_archive('cpt') || is_post_type_archive('industry')){
$query->set('order', 'ASC');
$query->set('orderby', 'menu_order');
$query->set('posts_per_page', 100);
}
});
// Show additional label for page in admin list view...
//add_filter( 'display_post_states', function( $post_states, $post ) {
// $archive_settings = get_field( 'region', 'option' );
// if( (int) $archive_settings['page'] === $post->ID ) {
// $post_states['page_for_regions'] = _x( 'Regions Archive', 'page label', 'nxw' );
// }
// return $post_states;
//}, 10, 2 );
add_filter('display_post_states', function($post_states, $post) {
$archive_ids = [59, 63];
foreach($archive_ids as $page_id){
if((int) $page_id === $post->ID) {
$post_states['page_for_'.get_post_field('post_name', $post->ID)] = get_the_title($post->ID).' Archive';
}
}
return $post_states;
}, 10, 2 );
// Register custom title placeholder
add_filter( 'enter_title_here', function( $title ) {
$screen = get_current_screen();
if ( 'team' == $screen->post_type ) {
$title = __( 'First and Last Name', 'wcn' );
}
return $title;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment