0

I have a custom post type registered: 'jobs'

// set up labels
    $labels = array(
        'name'               => 'Jobs',
        'singular_name'      => 'Job',
        'add_new'            => 'Add New Job',
        'add_new_item'       => 'Add New Job',
        'edit_item'          => 'Edit Job',
        'new_item'           => 'New Job',
        'all_items'          => 'All Jobs',
        'view_item'          => 'View Job',
        'search_items'       => 'Search Jobs',
        'not_found'          =>  'No Jobs Found',
        'not_found_in_trash' => 'No Jobs found in Trash',
        'menu_name'          => 'Jobs',
    );

    //register post type
    register_post_type( 'jobs', array(
        'labels'              => $labels,
        'has_archive'         => 'jobs',
        'public'              => true,
        'supports'            => array( 
            'title', 
            'editor', 
            'excerpt', 
            'thumbnail',
            'page-attributes' 
        ),
        'exclude_from_search' => false,
        'capability_type'     => 'post',
        'query_var'           => true,
        'menu_icon'           => 'dashicons-book-alt',
        'rewrite'             => array( 
            'slug'            => 'jobs/%job_status%',
            'with_front'      => false
        ),

        )

    );

And a custom Taxonomy: 'status'

$labels = array(
        'name'              => _x( 'Status', 'taxonomy general name', 'trademanager' ),
        'singular_name'     => _x( 'Status', 'taxonomy singular name', 'trademanager' ),
        'search_items'      => __( 'Search Statuses', 'trademanager' ),
        'all_items'         => __( 'All Statuses', 'trademanager' ),
        'parent_item'       => __( 'Parent Status', 'trademanager' ),
        'parent_item_colon' => __( 'Parent Status:', 'trademanager' ),
        'edit_item'         => __( 'Edit Status', 'trademanager' ),
        'update_item'       => __( 'Update Status', 'trademanager' ),
        'add_new_item'      => __( 'Add New Status', 'trademanager' ),
        'new_item_name'     => __( 'New Status Name', 'trademanager' ),
        'menu_name'         => __( 'Status', 'trademanager' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'jobs/%job_status%', 'with_front' => false ),
    );

I would like the urls to be: jobs/** specific status (like category) **/post

I found this function:

function tm_jobs_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'jobs' ){
        $terms = wp_get_object_terms( $post->ID, 'status' );
        if( $terms ){
            return str_replace( '%job_status%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}

add_filter( 'post_type_link', 'tm_jobs_permalinks', 1, 2 );

Which very nicely makes that url, however it just returns a 404 error. What am I doing wrong here?

  • Your post type and taxonomy have the same slug, your taxonomy slug shouldn’t contain the taxonomy query var, it should just be jobs – Milo Oct 8 '17 at 21:38
  • after every edit, you need to flush permalink before to try. it's what Solhan explains in his answer – mmm Oct 8 '17 at 22:07
1

Open Settings > Permalinks and just click on the Save Settings button without any changes.

  • Thanks for the reply Solhan but that isn't the solution. – lukgoh Oct 8 '17 at 21:43
0

Solved:

Changed taxonomy rewrite to:

'rewrite' => array( 'slug' => 'jobs', 'with_front' => false ),

Changed post type rewrite to:

'rewrite' => true

Added the following functions:

function tm_jobs_generating_rule($wp_rewrite) {
    $rules = array();
    $terms = get_terms( array(
        'taxonomy' => 'status',
        'hide_empty' => false,
    ) );

    $post_type = 'jobs';
    foreach ($terms as $term) {    

        $rules['jobs/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&jobs=$matches[1]&name=$matches[1]';

    }
    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'tm_jobs_generating_rule');

function tm_change_link( $permalink, $post ) {

    if( $post->post_type == 'jobs' ) {
        $resource_terms = get_the_terms( $post, 'status' );
        $term_slug = '';
        if( ! empty( $resource_terms ) ) {
            foreach ( $resource_terms as $term ) {
                // The featured resource will have another category which is the main one
                if( $term->slug == 'featured' ) {
                    continue;
                }
                $term_slug = $term->slug;
                break;
            }
        }
        $permalink = get_home_url() ."/jobs/" . $term_slug . '/' . $post->post_name;
    }
    return $permalink;
}
add_filter('post_type_link', 'tm_change_link',10,2);

All working correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.