Forked from anonymous/post-type-taxonomy-setup.php
Last active
September 27, 2017 17:16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Custom Post Type Setup */ | |
function post_type_jobs() { | |
$labels = array( | |
'name' => _x('Jobs', 'post type general name', 'agrg'), | |
'singular_name' => _x('Jobs', 'post type singular name', 'agrg'), | |
'add_new' => _x('Add New Job', 'jobs', 'agrg'), | |
'add_new_item' => __('Add New Job', 'agrg'), | |
'edit_item' => __('Edit Job', 'agrg'), | |
'new_item' => __('New Job', 'agrg'), | |
'view_item' => __('View Job', 'agrg'), | |
'search_items' => __('Search Jobs', 'agrg'), | |
'not_found' => __('No Jobs found', 'agrg'), | |
'not_found_in_trash' => __('No Jobs found in Trash', 'agrg'), | |
'parent_item_colon' => '' | |
); | |
$args = array( | |
'labels' => $labels, | |
'slug' => 'jobs', | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'query_var' => true, | |
'capability_type' => 'post', | |
'hierarchical' => true, | |
'menu_position' => null, | |
'menu_icon' => 'dashicons-menu', | |
'has_archive' => true, | |
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'page-attributes', 'post-formats' ) | |
); | |
register_post_type( 'jobs', $args ); | |
flush_rewrite_rules(false); | |
} | |
add_action('init', 'post_type_jobs'); | |
function be_register_taxonomies() { | |
$taxonomies = array( | |
array( | |
'slug' => 'job-department', | |
'single_name' => 'Department', | |
'plural_name' => 'Departments', | |
'post_type' => 'jobs', | |
'query_var' => true, | |
'public' => true, | |
), | |
array( | |
'slug' => 'job-type', | |
'single_name' => 'Type', | |
'plural_name' => 'Types', | |
'post_type' => 'jobs', | |
'query_var' => true, | |
'public' => true, | |
'hierarchical' => false, | |
), | |
array( | |
'slug' => 'job-experience', | |
'single_name' => 'Min-Experience', | |
'plural_name' => 'Min-Experiences', | |
'post_type' => 'jobs', | |
'query_var' => true, | |
'public' => true, | |
), | |
); | |
foreach( $taxonomies as $taxonomy ) { | |
$labels = array( | |
'name' => $taxonomy['plural_name'], | |
'singular_name' => $taxonomy['single_name'], | |
'search_items' => 'Search ' . $taxonomy['plural_name'], | |
'all_items' => 'All ' . $taxonomy['plural_name'], | |
'parent_item' => 'Parent ' . $taxonomy['single_name'], | |
'parent_item_colon' => 'Parent ' . $taxonomy['single_name'] . ':', | |
'edit_item' => 'Edit ' . $taxonomy['single_name'], | |
'update_item' => 'Update ' . $taxonomy['single_name'], | |
'add_new_item' => 'Add New ' . $taxonomy['single_name'], | |
'new_item_name' => 'New ' . $taxonomy['single_name'] . ' Name', | |
'menu_name' => $taxonomy['plural_name'] | |
); | |
$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] ); | |
$hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true; | |
register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array( | |
'hierarchical' => $hierarchical, | |
'labels' => $labels, | |
'show_ui' => true, | |
'query_var' => true, | |
'rewrite' => $rewrite, | |
)); | |
} | |
} | |
add_action( 'init', 'be_register_taxonomies', 0 ); | |
function lh_add_rewrite_rules() { | |
global $wp_rewrite; | |
$rules = array(); | |
$terms_dept = get_terms( array( | |
'taxonomy' => 'job-department', | |
'hide_empty' => false, | |
) ); | |
$terms_type = get_terms( array( | |
'taxonomy' => 'job-type', | |
'hide_empty' => false, | |
) ); | |
$post_type = 'jobs'; | |
foreach ( $terms_dept as $term_dept ) { | |
//Job Department Archive: https://grafdom.net/projects/sa/testing/jobs/administration | |
add_rewrite_rule('jobs/' . $term_dept->slug , 'index.php?post_type=' . $post_type . '&job-department=' . $term_dept->slug, 'top'); | |
foreach ( $terms_type as $term_type ) { | |
//Job Type Archive: https://grafdom.net/projects/sa/testing/jobs/part-time | |
add_rewrite_rule('jobs/' . $term_type->slug, 'index.php?post_type=' . $post_type . '&job-type=' . $term_type->slug, 'top'); | |
//Job Department/Type Archive: https://grafdom.net/projects/sa/testing/jobs/administration/part-time | |
add_rewrite_rule('jobs/' . $term_dept->slug . '/' . $term_type->slug, 'index.php?post_type=' . $post_type . '&job-department=' . $term_dept->slug . '&job-type=' . $term_type->slug, 'top'); | |
} | |
} | |
} | |
add_action('init', 'lh_add_rewrite_rules'); | |
//Test my rewrite rules | |
function test_rwr_foot() { | |
//https://grafdom.net/projects/sa/testing/wp-admin/tools.php?page=monkeyman-rewrite-analyzer | |
global $wp_rewrite; | |
echo '<pre>'; | |
print_r( $wp_rewrite->rules ); | |
echo '</pre>'; | |
} | |
add_action( 'wp_footer','test_rwr_foot' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment