Search code examples
wordpresscustom-post-typecustom-taxonomy

Get post by category id in custom post type with custom taxonomy


This is my script for create custom post type and its taxonomy. It created successfully bust post fetching problem by category id. In my category I have 3 post but can't get any one post.

For create code :

function man_ourteam_custom_init() {
    $man_labels = array(
        'name' => 'Member',
        'singular_name' => 'ourteam',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Member',
        'edit_item' => 'Edit Member',
        'new_item' => 'New Member',
        'all_items' => 'All Member',
        'view_item' => 'View Member',
        'search_items' => 'Search Member',
        'not_found' => 'No Member found',
        'not_found_in_trash' => 'No Member found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'Our Team'
    );
    $man_args = array(
        'labels' => $man_labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'ourteam'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title', 'editor', 'thumbnail')     
    );
    register_post_type('ourteam', $man_args);

   register_taxonomy("member-category", array("ourteam"), array("hierarchical" => true, 'show_admin_column' => true,
        "label" => "Member Categories",
        "singular_label" => "Member Categories",
        "rewrite" => array('slug' => 'ourteam')));
}

add_action('init', 'man_ourteam_custom_init');

For post get code :

My category id is 191 it's have 3 posts.

$ourteam_category_check = '191';

            $niche_ourteam_args = array(
                'post_type' => 'ourteam',
                'cat'   => $ourteam_category_check,                 
                'orderby' => 'post_date',               
                'order' => 'DESC',
                'post_status' => 'publish',
                'meta_query' => array(
                    array(
                        'key' => '_thumbnail_id',
                        'compare' => 'EXISTS'
                    ),
                )
            );          

            $niche_ourteam = new WP_Query($niche_ourteam_args);

            while ($niche_ourteam->have_posts()) : $niche_ourteam->the_post();

                /*** My loop code ***/

            endwhile;

Solution

  • You need to add 'tax_query', not 'cat'..

    $ourteam_category_check = '191';
    
        $niche_ourteam_args = array(
                'post_type' => 'ourteam',
               // 'cat'   => $ourteam_category_check,                 
                'orderby' => 'post_date',               
                'order' => 'DESC',
                'post_status' => 'publish',
                'meta_query' => array(
                    array(
                        'key' => '_thumbnail_id',
                        'compare' => 'EXISTS'
                    ),
                ),
           'tax_query' => array(
                array(
                    'taxonomy' => 'member-category',
                    'field' => 'term_id',
                    'terms'    => $ourteam_category_check
                ),
           ),
       );          
    
            $niche_ourteam = new WP_Query($niche_ourteam_args);
    
            while ($niche_ourteam->have_posts()) : $niche_ourteam->the_post();
    
                /*** My loop code ***/
    
            endwhile;