0

I created several Custom Post Types and I also created a category too. I assigned several custom posts to that category and I tried to call it to display the posts in home page, but it's not displaying anything even though it's displaying posts from the Posts in wp dashboard menu.

Here's my code in which I called the posts in template-home.php

<?php 
    $args = array (
        'cat'              => 3,
        'posts_per_page'         => 3
    );

     $the_query = new WP_Query( $args ); ?>
            <?php if ( $the_query->have_posts() ) : ?>

        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 
           /* Display code */
        <?php endwhile;  ?>

    <?php endif; ?>

Here's also one of my custom post type, TV in functions.php:

// Register Custom Post Type
function tvs_post_type() {

    $labels = array(
        'name'                  => _x( 'TV', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'TV', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'TV', 'text_domain' ),
        'name_admin_bar'        => __( 'Post Type', 'text_domain' ),
        'archives'              => __( 'Item Archives', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent TV:', 'text_domain' ),
        'all_items'             => __( 'All TVs', 'text_domain' ),
        'add_new_item'          => __( 'Add New Post', 'text_domain' ),
        'add_new'               => __( 'Add New Post', 'text_domain' ),
        'new_item'              => __( 'New Item', 'text_domain' ),
        'edit_item'             => __( 'Edit Post', 'text_domain' ),
        'update_item'           => __( 'Update Post', 'text_domain' ),
        'view_item'             => __( 'View Post', 'text_domain' ),
        'search_items'          => __( 'Search Posts', 'text_domain' ),
        'not_found'             => __( 'No posts found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not posts found in Trash', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
        'items_list'            => __( 'Items list', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'TV', 'text_domain' ),
        'description'           => __( 'tv information pages', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'custom-fields', ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,        
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'tv', $args );

}
add_action( 'init', 'tvs_post_type', 0 );

However, I still can't figure out why it's not displaying when I assigned some posts to the category and try to call them. All I know is that it was able to display the posts from the Posts menu in wp dashboard when I assigned them to the category but not from the Custom Post types I've created...

1

Add post_type to first $args

<?php 
    $args = array (
        'cat'              => 3,
        'post_type'        => 'tv',
        'posts_per_page'   => 3
    );

     $the_query = new WP_Query( $args ); ?>
            <?php if ( $the_query->have_posts() ) : ?>

        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 
           <?php the_title(); ?>
        <?php endwhile;  ?>

    <?php endif;
     wp_reset_postdata();
    ?>
  • Okay, thanks, it works. but why isn't it automatically added when 'taxonomies' => array( 'category', 'post_tag' ),' was already added to the custom post type? – Kristina Bressler Mar 31 '17 at 2:03
  • That part adds the mentioned taxonomies to your custom post type. But you're using WP_Query here which has default post type set to post – Abdul Awal Mar 31 '17 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.