0

I'm posting this as I found the problem, but it took me ages to find it with the mass of 'Refresh your permalinks' answers.

<?php
add_action( 'init', 'mytheme_create_post_type' );
function mytheme_create_post_type() {  // jobs custom post type
    // set up labels
    $job_labels = array(
        'name' => __( 'Jobs' ),
        'singular_name' => __( 'Job' ),
        'add_new' => __( 'Add New' ),
        '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' ),
        'parent_item'       => __( 'Parent Job' ),
        'parent_item_colon' => __( 'Parent Job:' ),
        'menu_name' => __( 'Jobs' ),
    );
    register_post_type(
        'job',
        array(
            'labels' => $job_labels,
            'has_archive' => true,
            'public' => true,
            'hierarchical' => false,
            'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail', 'page-attributes' ),
            'taxonomies' => array( 'post_tag', 'job_category' ),
            'exclude_from_search' => true,
            'capability_type' => 'post'
        )
    );

    register_taxonomy_for_object_type( 'job_category', 'job' );
}

add_action( 'init', 'mytheme_create_taxonomies', 5 );
function mytheme_create_taxonomies() {
    // job taxonomy
    $job_labels = array(
        'name'              => _x( 'Job Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Job Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Job Categories' ),
        'all_items'         => __( 'All Job Categories' ),
        'parent_item'       => __( 'Parent Job Category' ),
        'parent_item_colon' => __( 'Parent Job Category:' ),
        'edit_item'         => __( 'Edit Job Category' ),
        'update_item'       => __( 'Update Job Category' ),
        'add_new_item'      => __( 'Add New Job Category' ),
        'new_item_name'     => __( 'New Job Category' ),
        'menu_name'         => __( 'Job Categories' ),
    );
    register_taxonomy(
        'job_category',
        'job',
        array(
            'hierarchical' => false,
            'labels' => $job_labels,
            'rewrite' => false,
            'show_admin_column' => true
        )
    );
}

Given the problems with permalinks I'd turned everything off. I'd set Settings > Permalinks to default and set rewrite => false for both the post type and the taxonomy.

However after creating a Job with a category of HR going to http://www.example.com/?job_category=hr gave me a 'Sorry no results found' i.e. not a 404 but no results for that category.

  • 1
    Why the redundant call to register_taxonomy_for_object_type? – Tom J Nowell Apr 25 '15 at 2:54
  • ah that was part of my scrambling around searching for the problem from the register_taxonomy codex: "Better be safe than sorry when registering custom taxonomies for custom post types. Use register_taxonomy_for_object_type() right after the function to interconnect them. Else you could run into minetraps where the post type isn't attached inside filter callback that run during parse_request or pre_get_posts." – icc97 Apr 25 '15 at 3:37
1

The problem was 'exclude_from_search' => true in the register_post_type options.

Given that setting 'public' => true defaults to exluding from search by default I assumed that this was the correct setting.

However this is in the WordPress codex for register_post_type exclude_from_search:

Note: If you want to show the posts's list that are associated to taxonomy's terms, you must set exclude_from_search to false (ie : for call site_domaine/?taxonomy_slug=term_slug or site_domaine/taxonomy_slug/term_slug). If you set to true, on the taxonomy page (ex: taxonomy.php) WordPress will not find your posts and/or pagination will make 404 error...

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.