WordPress show posts from custom post type with a category

I am using the CPT UI plugin which i have created a custom post type with (called Knowledgebase and the taxonomy called knowledgebase-categories)

i am using this code to display posts:

Read More
<?php $query = new WP_Query( array('post_type' => 'knowledgebase', 'posts_per_page' => 20, 'category_name' => 'Cisco' ) ); ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a href=""><?php the_title(); ?></a>
            </article> <!-- .et_pb_post -->
        <?php endwhile; ?>

it works fine without the category_name but with the above code its not showing any posts

there are posts with a category of Cisco

Related posts

3 comments

  1. May this will Help you

    <?php $query = new WP_Query( array( 
            'post_type' => 'knowledgebase', 
            'cat' => 5, // Whatever the category ID is for your aerial category
            'posts_per_page' => 10,
            'orderby' => 'date', // Purely optional - just for some ordering
            'order' => 'DESC' // Ditto
        ) );
    
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
  2. Here you can achieve by this

    $args = array(
        'post_type' => 'knowledgebase',
        'tax_query' => array(
            array(
                'taxonomy' => 'knowledgebase-categories',
                'field'    => 'slug',
                'terms'    => 'knowledgebase-terms',
            ),
        ),
    );
    $query = new WP_Query( $args );
    
  3. because they are posts from a custom post type i had to change category_name to the taxonomy (which is knowledgebase-categories)

Comments are closed.