• I am not certain what I have wrong. I just need to display the posts from a custom post type and/or a single category.

    I have a custom post type called “videos” that I want to display the posts from outside the basic loop. That loop is already displaying properly on the page. However, when this loop is reached, it displays all of the Posts again, but it does not include this custom post type at all. It’s showing every post I do not want instead of the ones that I do.

    I have tried to use the same code to filter by category (the default WordPress categories, not a custom taxonomy) and it does not work in that way either.

    <?php
     
     // The Query
     $the_query = new WP_Query( array( 'post_type' => 'videos' ) );
      
     // The Loop
     if ( $the_query->have_posts() ) {
    	 echo '<ul>';
    	 while ( $the_query->have_posts() ) {
    		 $the_query->the_post();
    		 echo '<li>' . get_the_title() . '</li>';
    	 }
    	 echo '</ul>';
     } else {
    	 echo("Broken");
     }
     /* Restore original Post Data */
     wp_reset_postdata(); ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • I don’t see any issues with this code.

    Where are you trying to run this code? Do you have any pre_get_posts in the theme or plugin? Have you tried running this with plugins disabled and in a default theme?

    Thread Starter WatcherJoshua

    (@watcherjoshua)

    @howdy_mcgee

    The code is included in index.php on the home page of the website. The page includes the normal WordPress loop and then this query below that (where I want to show posts from only one post type that is excluded from the normal loop).

    I just tried disabling all plugins and using the Twentynineteen theme with the same results: It displays all the same stories from the normal loop again and does not include any from the custom post type. It’s as if I had just included the same loop twice.

    Are you running this query inside the main loop? If your index.php isn’t huge it may be beneficial to post the whole file.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display only posts from a specific category or post type’ is closed to new replies.