Skip to content

Instantly share code, notes, and snippets.

@logichub
Forked from anonymous/post-type-taxonomy-setup.php
Last active September 27, 2017 17:16
Show Gist options
  • Select an option

  • Save logichub/461b6c59c2e88f3bab3b03759434fcdc to your computer and use it in GitHub Desktop.

Select an option

Save logichub/461b6c59c2e88f3bab3b03759434fcdc to your computer and use it in GitHub Desktop.

Revisions

  1. logichub revised this gist Sep 27, 2017. 1 changed file with 103 additions and 78 deletions.
    181 changes: 103 additions & 78 deletions post-type-taxonomy-setup.php
    Original file line number Diff line number Diff line change
    @@ -2,30 +2,31 @@
    /* 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' => ''
    '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,
    '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' )
    '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 );
    @@ -35,72 +36,96 @@ function post_type_jobs() {

    function be_register_taxonomies() {
    $taxonomies = array(
    array(
    'slug' => 'job-department',
    'single_name' => 'Department',
    'plural_name' => 'Departments',
    'post_type' => 'jobs',
    'rewrite' => array( 'slug' => 'department' ),
    ),
    array(
    'slug' => 'job-type',
    'single_name' => 'Type',
    'plural_name' => 'Types',
    'post_type' => 'jobs',
    'hierarchical' => false,
    'rewrite' => array( 'slug' => 'type' ),
    ),
    array(
    'slug' => 'job-experience',
    'single_name' => 'Min-Experience',
    'plural_name' => 'Min-Experiences',
    'post_type' => 'jobs',
    'rewrite' => array( 'slug' => 'experience' ),
    ),
    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']
    );
    $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;
    $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,
    ));
    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 );

    /* Rewrite Rule */
    /*function custom_rewrite_rules() {
    add_rewrite_rule('^job-department/(.*)/job-type/(.*)?', 'index.php?job-department=$matches[1]&job-type=$matches[2]', 'top');
    }
    add_action('init', 'custom_rewrite_rules');*/

    function lh_add_rewrite_rules() {
    global $wp_rewrite;
    $rules = array();

    $new_rules = array(
    'jobs/(department|type)/(.+?)/(department|type)/(.+?)/?$' => 'index.php?post_type=jobs&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4),
    'jobs/(department|type)/(.+)/?$' => 'index.php?post_type=jobs&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2)
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    $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( 'generate_rewrite_rules', 'lh_add_rewrite_rules' );
    add_action( 'wp_footer','test_rwr_foot' );
  2. logichub revised this gist Sep 27, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion post-type-taxonomy-setup.php
    Original file line number Diff line number Diff line change
    @@ -97,7 +97,6 @@ function be_register_taxonomies() {
    function lh_add_rewrite_rules() {
    global $wp_rewrite;

    add_rewrite_rule('^job-department/(.*)/job-type/(.*)?', 'index.php?job-department=$matches[1]&job-type=$matches[2]', 'top');
    $new_rules = array(
    'jobs/(department|type)/(.+?)/(department|type)/(.+?)/?$' => 'index.php?post_type=jobs&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4),
    'jobs/(department|type)/(.+)/?$' => 'index.php?post_type=jobs&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2)
  3. logichub revised this gist Sep 27, 2017. 1 changed file with 17 additions and 2 deletions.
    19 changes: 17 additions & 2 deletions post-type-taxonomy-setup.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    /* Custom Post Type Setup */
    function post_type_jobs() {
    $labels = array(
    @@ -47,12 +48,14 @@ function be_register_taxonomies() {
    'plural_name' => 'Types',
    'post_type' => 'jobs',
    'hierarchical' => false,
    'rewrite' => array( 'slug' => 'type' ),
    ),
    array(
    'slug' => 'job-experience',
    'single_name' => 'Min-Experience',
    'plural_name' => 'Min-Experiences',
    'post_type' => 'jobs',
    'rewrite' => array( 'slug' => 'experience' ),
    ),
    );
    foreach( $taxonomies as $taxonomy ) {
    @@ -86,7 +89,19 @@ function be_register_taxonomies() {
    add_action( 'init', 'be_register_taxonomies', 0 );

    /* Rewrite Rule */
    function custom_rewrite_rules() {
    /*function custom_rewrite_rules() {
    add_rewrite_rule('^job-department/(.*)/job-type/(.*)?', 'index.php?job-department=$matches[1]&job-type=$matches[2]', 'top');
    }
    add_action('init', 'custom_rewrite_rules');
    add_action('init', 'custom_rewrite_rules');*/

    function lh_add_rewrite_rules() {
    global $wp_rewrite;

    add_rewrite_rule('^job-department/(.*)/job-type/(.*)?', 'index.php?job-department=$matches[1]&job-type=$matches[2]', 'top');
    $new_rules = array(
    'jobs/(department|type)/(.+?)/(department|type)/(.+?)/?$' => 'index.php?post_type=jobs&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4),
    'jobs/(department|type)/(.+)/?$' => 'index.php?post_type=jobs&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2)
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    add_action( 'generate_rewrite_rules', 'lh_add_rewrite_rules' );
  4. @invalid-email-address Anonymous created this gist Sep 27, 2017.
    92 changes: 92 additions & 0 deletions post-type-taxonomy-setup.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    /* 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,
    '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',
    'rewrite' => array( 'slug' => 'department' ),
    ),
    array(
    'slug' => 'job-type',
    'single_name' => 'Type',
    'plural_name' => 'Types',
    'post_type' => 'jobs',
    'hierarchical' => false,
    ),
    array(
    'slug' => 'job-experience',
    'single_name' => 'Min-Experience',
    'plural_name' => 'Min-Experiences',
    'post_type' => 'jobs',
    ),
    );
    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 );

    /* Rewrite Rule */
    function custom_rewrite_rules() {
    add_rewrite_rule('^job-department/(.*)/job-type/(.*)?', 'index.php?job-department=$matches[1]&job-type=$matches[2]', 'top');
    }
    add_action('init', 'custom_rewrite_rules');