Show only post of custom category

I have a custom post types called photo gallery. In that post type I have registered a taxonomy called video. Then i have created two category under video called ‘personal’ & ‘commercial’. Now i want to show only the commercial category post to a page section. How can I do that? Here is the code I have tried but not working

                      <?php 

                            $args = array(
                        'post_type'=>'photo_gallerys',
                        'post_status'=>'publish',
                        'video'=>'commercial',
                        'posts_per_page'=>-1,
                        'paged'=>get_query_var('paged')
                         );
// the query
$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>

    <!-- pagination here -->

    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

       <?php the_title(); ?>


    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Related posts

Leave a Reply

2 comments

  1. Try this code hope you’ll find your solution

        <?php
        $tax_post_args = array(
                'post_type' => 'your post type name',
                'posts_per_page' => 999,
                'order' => 'ASC',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'your taxonomy name',
                        'field' => 'id',
                        'terms' => your category id
                    )
                )
            );
    
    $tax_post_qry = new WP_Query($tax_post_args);
    
        while($tax_post_qry->have_posts()) :
               $tax_post_qry->the_post();
               the_title();
        endwhile;
        ?>
    

    if you want to get posts from category slug then use this code in tax_query array

    'field' => 'slug',
    'terms' => 'your category slug'
    
  2. use 'cat' => "your_cat_id in your wp query argument.

    for example if I want to display post for only commercial category and commercial have category id 21, then I will write query to display post like this:

    <?php 
     $args = array(
    'post_type'=>'photo_gallerys',
    'post_status'=>'publish',
    'video'=>'commercial',
    'cat' => 21,
    'posts_per_page'=>-1,
    'paged'=>get_query_var('paged')
     );