WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Post Type Posts not Displayed (6 posts)

  1. Val Lynn
    Member
    Posted 1 year ago #

    What I thought: Create a new CPT, use default categories and tags, and CPT posts will show up like everything else. If the latest post was of CPT Articles, it would show up on the front page, first...

    But no, that does not happen.

    Clicking the one category to which a CPT Articles post is assigned yields everything but the CPT Articles posts!

    Clicking either of the two tags to which my CPT Articles post is assigned yields anything but my CPT Article posts.

    I can View the Post from the Admin panel.

    I created the Custom Post Type ("Articles") with this taxonomy:

    'taxonomies' => array('category', 'post_tag'), // just use default categories and tags

    and, leaving index.php, single.php and article.php in the sub-directory, I created and uploaded single-my_articles.php and archive-my_articles.php files with

    <?php /* Start the Loop */ ?>
    <?php // VAL MOD for Custom Post Type
      $loop = new WP_Query( array( 'post_type' => 'my_articles', 'posts_per_page' => 10 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php
      /* Include the Post-Format-specific template for the content.
       * If you want to overload this in a child theme then include a file
       * called content-___.php (where ___ is the Post Format name) and that will be used instead.  */
      get_template_part( 'content', get_post_format() );
     ?>
     <?php endwhile; ?>

    I am unable to display any of the posts I have created using this new Articles CPT.

    Full code at pastebin

    I am missing something very obvious...

  2. bcworkz
    Member
    Posted 1 year ago #

    You need to add 'post-formats' to $args['supports'] array definition.

  3. Val Lynn
    Member
    Posted 1 year ago #

    Thank you for replying.

    Here is the current supports array:
    'supports' => array( 'title', 'editor', 'comments', 'revisions', 'author', 'excerpt', 'custom-fields', 'thumbnail', 'post-formats' ),

    The posts from the Custom Post Type are still not returned when I click the Category link the posts are associated with or when I click either of the Tags associated with the posts.

    As a point of discussion, I thought child themes inherited the post formats of their parents. I am not seeing how the addition of this argument to register_post_type is relevant to the issue?

  4. Val Lynn
    Member
    Posted 1 year ago #

    @bcworkz, thank you for your help.

    I'm documenting my newbie thought process and how I found the solution for the other WordPress wanderers out there with the hope this makes another's WordPress learning journey a little shorter.

    What I thought: Create a new CPT, use default categories and tags, and CPT posts will show up like everything else. If the latest post was of CPT Articles, it would show up on the front page, first...

    What I've learned (so far): Custom Post Types (CPT) do not "show up like everything else."

    Why? Because CPT's are not "posts." (Note to Dev's: Thank you. Love you all! IMHO, the result of that discussion you had about what to name this "thing" should have resulted in "it" being named something else.

    Logically, those who argued to keep the phrase "Custom Post Types," are technically correct, while those who argued to name it anything else on the grounds that naming it "Custom Post Type" would be confusing, were, "practically correct." I know there's a term for that but it slips my mind right now.)

    The solution to my problem begins with understanding that Custom Post Types do not show up automatically like blog posts because they are not blog posts.

    Thank you Otto for your post about Custom Post Types which stated...

    ... they are still not Posts.
    They do not show up on the Blog.
    They do not appear in the Feed.

    This explains why, out-of-the-box, my shiny new CPT was not magically appearing on the home page of my site. This was not obvious to me and I could not find anything in the Codex that said, if one creates a CPT, one will have to do some work to make them appear on the home page and in an RSS feed. It might be helpful to put that blurb in the Codex to help light the way for the unenlightened.

    What Solved the Problem: Juston Tadlock's short tutorial Showing Custom Post Types on Your Blog solved my problem.

    Essentially, I had to add another function to my functions.php file:

    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    		function my_get_posts( $query ) {
    
    			if ( ( is_home() && $query->is_main_query() ) || is_feed() )
    			$query->set( 'post_type', array( 'post', 'my_articles' ) );
    
    		return $query;
    		}
  5. racl101
    Member
    Posted 1 year ago #

    @Val Lynn

    Thanks for posting your experience. Justin Tadlock's tutorial definitely helped me get it in perspective. I had the same problem and the code snippet fixed.

    He's so awesome with WordPress!

    RC

  6. nicholascooper
    Member
    Posted 1 year ago #

    Thank you for sharing

Topic Closed

This topic has been closed to new replies.

About this Topic