Question

i have created

  1. custom post type: jobs
  2. custom taxonomy : jobs_division

I wanted url like Custom permalink structure: /%custom-post-type%/%custom-taxonomy%/%post-name%/

I have registered custom post type using the following code using plugin and i am using the twenty fifteen theme

Every thing works fine except for i.e

example.com/jobs

should show all the posts

but it says page not found

here is the code

add_action( 'init', 'boss_cpt' );

function boss_cpt() {
  $labels = array(
    'name'               => 'Jobs',
    'singular_name'      => 'Job',
    'menu_name'          => 'Jobs',
    'name_admin_bar'     => 'Job',
    'add_new'            => 'Add New', 'Job',
    'add_new_item'       => 'Add New Job',
    'new_item'           => 'New Job',
    'edit_item'          => 'Edit Job',
    'view_item'          => 'View Job',
    'all_items'          => 'All Jobs',
    'search_items'       => 'Search Jobs',
    'parent_item_colon'  => 'Parent Jobs:',
    'not_found'          => 'No Jobs found.',
    'not_found_in_trash' => 'No Jobs found in Trash.'
  );

  $args = array(
    'labels'             => $labels,
        'description'        => __( 'Jobs' ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite' => array(
    'slug' => 'jobs/%jobs_division%',
    'with_front' => true
     ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => true,
    'menu_position'      => 20,
    'supports'           => array( 'title', 'editor', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'thumbnail', 'author', 'page-attributes' ),
    'taxonomies'         => array( 'category' )
  );

  register_post_type( 'jobs', $args );

}

add_action( 'init', 'job_taxonomies', 0 );

function job_taxonomies() {
    register_taxonomy(
        'jobs_division',
        'jobs',
        array(
            'labels'      => array(
                'name'      => 'Job Division',
                'add_new_item'  => 'Add New Job Division',
                'new_item_name' => "New Job Division"
            ),
            'show_ui'       => true,
            'show_tagcloud'   => false,
            'hierarchical'    => true,
            'public'      => true,
            'show_ui'       => true,
            'query_var'     => 'true',
            'rewrite' => array(
    'slug' => 'jobs',
    'with_front' => true
  ),
            )
        );
}
/**
 * Tell WordPress how to interpret our jobs URL structure
 *
 * @param array $rules Existing rewrite rules
 * @return array
 */
function so23698827_add_rewrite_rules( $rules ) {
  $new = array();
  $new['jobss/([^/]+)/(.+)/?$'] = 'index.php?jobs=$matches[2]';
  $new['jobss/(.+)/?$'] = 'index.php?jobs_division=$matches[1]';

  return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );

/**
 * Handle the '%jobs_division%' URL placeholder
 *
 * @param str $link The link to the post
 * @param WP_Post object $post The post object
 * @return str
 */
function so23698827_filter_post_type_link( $link, $post ) {
  if ( $post->post_type == 'jobs' ) {
    if ( $cats = get_the_terms( $post->ID, 'jobs_division' ) ) {
      $link = str_replace( '%jobs_division%', current( $cats )->slug, $link );
    }
  }
  return $link;
}
add_filter( 'post_type_link', 'so23698827_filter_post_type_link', 10, 2 );

I will be really grateful if you can help me out

Was it helpful?

Solution

this worked for me

'has_archive'=> true,

replace it with

'has_archive'=> 'jobs',

then flushed the permalinks

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top