Search code examples
wordpresscategoriestaxonomy

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; ?>

Solution

  • 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'