0

I am using the following code to create a Custom Post Type and a Custom Taxonomy:

// Register Post Type
add_action( 'init', 'cptui_register_my_cpt_resource' );
function cptui_register_my_cpt_resource() {
    register_post_type( 'resource', array(
        'label' => 'Resources',
        'description' => '',
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => false,
        'rewrite' => array('slug' => 'resource', 'with_front' => true),
        'query_var' => true,
        'supports' => array( 'title', 'editor', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'thumbnail', 'author', 'page-attributes',' post-formats' ),
        'labels' => array (
            'name' => 'Resources',
            'singular_name' => 'Resource',
            'menu_name' => 'Resources',
            'add_new' => 'Add Resource',
            'add_new_item' => 'Add New Resource',
            'edit' => 'Edit',
            'edit_item' => 'Edit Resource',
            'new_item' => 'New Resource',
            'view' => 'View Resource',
            'view_item' => 'View Resource',
            'search_items' => 'Search Resources',
            'not_found' => 'No Resources Found',
            'not_found_in_trash' => 'No Resources Found in Trash',
            'parent' => 'Parent Resource',
        )
    )); 
}

// Register Taxonomy 
add_action( 'init', 'cptui_register_my_taxes_resource_categories' );
function cptui_register_my_taxes_resource_categories() {
    register_taxonomy( 'resource categories', array( 0 => 'resource' ), array( 
        'hierarchical' => true,
        'label' => 'Resource Categories',
        'show_ui' => true,
        'query_var' => true,
        'show_admin_column' => false,
        'labels' => array (
            'search_items' => 'Resource Category',
            'popular_items' => '',
            'all_items' => '',
            'parent_item' => '',
            'parent_item_colon' => '',
            'edit_item' => '',
            'update_item' => '',
            'add_new_item' => '',
            'new_item_name' => '',
            'separate_items_with_commas' => '',
            'add_or_remove_items' => '',
            'choose_from_most_used' => '',
        )
    )); 
}

Everything appears to be working on the surface, I can connect a category to a resource, but when I try to query them as such:

$args = array(
    'suppress_filters' => false,
    'numberposts' => -1,
    'post_type' => 'resource',
    'category_name' => 'recruiting'

);
$the_query = new WP_Query( $args );

I don't get any results. Any ideas?

1
  • taxonomy names can't contain spaces, only lower case letters and underscore are valid characters.
    – Milo
    Commented Nov 11, 2014 at 17:08

2 Answers 2

0

What is your Custom Taxonomy name?

Try the following code, replace taxonomy_name with whatever your actual taxonomy name is.

$args = array(
    'suppress_filters' => false,
    'numberposts' => -1,
    'post_type' => 'resource',
    'tax_query' => array(
        array(
            'taxonomy' => 'taxonomy_name',
            'field'    => 'slug', //Select taxonomy term by slug( you can also use name/id)
            'terms'    => 'term-slug/category -slug',
        ),

);

$the_query = new WP_Query( $args );

Custom taxonomy parameter

0

Ah I did not use the custom taxonomy NAME which is "resource categories", an apparently those can contain spaces because this worked:

$args = array(
    'suppress_filters' => false,
    'numberposts' => 3,
    'post_type' => 'resource',
    'resource categories' => 'recruiting'
);
1
  • I imagine you may run into trouble down the rode with spaces in your taxonomy. You really should use underscores.
    – Howdy_McGee
    Commented Nov 11, 2014 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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