Custom post type
// Case studies custom post type
// based on https://wisdmlabs.com/blog/add-taxonomy-term-custom-post-permalinks-wordpress/
add_action('init', 'projects_cpt');
function projects_cpt() {
$labels = array(
'name' => 'Case studies',
'singular_name' => 'Case study',
'add_new' => 'Add New',
'add_new_item' => 'Add New Case study',
'edit_item' => 'Edit Case study',
'new_item' => 'New Case study',
'all_items' => 'All Case studies',
'view_item' => 'View Case study',
'search_items' => 'Search Case studies',
'not_found' => 'No Case studies found',
'not_found_in_trash' => 'No Case studies found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Case studies'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'taxonomies' => array('industrytype'),
'rewrite' => array('slug' => 'industries/%casestudycategory%/case-studies', 'with_front' => false),
//Adding custom rewrite tag
'capability_type' => 'post',
//'has_archive' => 'projectsarchives',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt'),
);
register_post_type('casestudy', $args);
$labels = array(
‘name’ => ‘Case study Categories’,
‘singular_name’ => ‘Case studies’,
‘search_items’ => ‘Search Case study Categories’,
‘all_items’ => ‘All Case study Categories’,
‘parent_item’ => ‘Parent Case study Category’,
‘parent_item_colon’ => ‘Parent Case study Category:’,
‘edit_item’ => ‘Edit Case study Category’,
‘update_item’ => ‘Update Case study Category’,
‘add_new_item’ => ‘Add New Case study Category’,
‘new_item_name’ => ‘New Case study Category’,
);
$args = array(
‘hierarchical’ => true,
‘rewrite’ => array(‘slug’ => ‘case-studies’),
‘show_in_nav_menus’ => true,
‘show_admin_column’ => true,
‘labels’ => $labels
);
register_taxonomy(‘casestudycategory’, ‘casestudy’, $args);
unset($labels);
unset($args);
}
add_filter(‘post_type_link’, ‘projectcategory_permalink_structure’, 10, 4);
function projectcategory_permalink_structure($post_link, $post, $leavename, $sample) {
if (false !== strpos($post_link, ‘%casestudycategory%’)) {
// get all terms
$casestudycategory_type_term = get_the_terms($post->ID, ‘casestudycategory’);
if (!empty($casestudycategory_type_term)){
// find primary term
$primarycat = ‘x’;
$primarray = get_post_meta($post->ID,’_yoast_wpseo_primary_casestudycategory’);
foreach ( $casestudycategory_type_term as $term ) {
if( $primarray[0] == $term->term_id ) {
// this is a primary category
$primarycat = $term->slug;
}
}
if ($primarycat == ‘x’) {
$primarycat = array_pop($casestudycategory_type_term)->slug;
}
$post_link = str_replace(‘%casestudycategory%’, $primarycat, $post_link);
} else {
$post_link = str_replace(‘%casestudycategory%’, ‘uncategorized’, $post_link);
}
}
return $post_link;
}
// redirect automatic-casestudy-archive pages to actual pages
function custom_event_rewrite_one() {
add_rewrite_rule( ‘^industries/([^/]+)/case-studies/?$’,
‘index.php?pagename=industries%2F$matches[1]%2Fcase-studies’,
‘top’);
add_rewrite_rule( ‘^industries/([^/]+)/?$’,
‘index.php?pagename=industries%2F$matches[1]’,
‘top’);
}
add_action(‘init’,’custom_event_rewrite_one’);
Shortcode to display list of custom posts
// shortcode to list case studies with option to filter by casestudy category [listcasestudies cscat="automotive"]
function cdk_get_casestudies( $atts ) {
$atts = shortcode_atts( array(
‘cscat’ => ”
), $atts );
$args = [
‘post_type’ => ‘casestudy’,
‘posts_per_page’ => ‘-1’,
‘order’ => ‘DESC’,
‘orderby’ => ”,
];
if ( $atts[‘cscat’] ) {
$args[‘tax_query’] = [
[
‘taxonomy’ => ‘casestudycategory’,
‘field’ => ‘slug’,
‘terms’ => array( sanitize_title( $atts[‘cscat’] ) )
]
];
}
$the_query2 = new WP_Query( $args );
$output2 = ”;
if ( $the_query2->have_posts() ) {
while ( $the_query2->have_posts() ) {
$the_query2->the_post();
$terms = get_the_terms( get_the_ID(), ‘casestudycategory’ );
$all_cats = array();
$primarycat = ‘x’;
$primarray = get_post_meta(get_the_ID(),’_yoast_wpseo_primary_casestudycategory’);
if ( $terms && !is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$all_cats[] = $term->name . ‘(‘.$term->term_id.’)’;
if( $primarray[0] == $term->term_id ) {
// this is a primary category
$primarycat = $term->slug;
}
}
}
$all_cats_str = join( “, “, $all_cats );
$first_cat_slug = $terms[0]->slug;
if ($primarycat == ‘x’) {
$primarycat = $first_cat_slug;
}
if ( $atts[‘cscat’] ) { // display for specific category
$output2 .= ‘<p> * <a href=”‘ . get_permalink() .'”>’. get_the_title() .'</a></p>’;
} else {
$output2 .= ‘<p> * <a href=”‘ . get_permalink() .'”>’. get_the_title() .'</a></p>’;
}
$output2 .= ‘<p>primary cat = ‘.$primarycat.’
<br>all = ‘.$all_cats_str.'</p>’;
}
wp_reset_postdata();
} else {
$output2 .= ‘<p>No case studies…</p>’;
}
return $output2;
}
add_shortcode(‘listcasestudies’, ‘cdk_get_casestudies’);