0

I could use another set of eyes on this one...its been a little bit of time since I last created a Wordpress theme. I can't figure out what I'm doing wrong here.

I created a custom post type (journal) and set up a loop on a custom template, which I set on the homepage.
I then have single-journal.php template for the individual post types.
The Journal CPT posts show up fine in my home page template, the url/permalink looks correct on inspection. However, when I click through on any of them (I have three at the moment), they all show the content of the latest post. The url/permalink comes up correct in the url bar too, but the content is wrong. Heres some code: This is the loop on the home page template:

<div class="post-slider">
                        <?php $args = array('post_type' => 'journal', 
                        'posts_per_page' => -1,
                        'post_status' => 'publish',
                        'orderby' => 'date',
                        'order' => 'DESC'); 
                                $loop = new WP_Query($args); 
                             if ( $loop->have_posts() ) : 
                                while ( $loop->have_posts() ) : 
                                    $loop->the_post(); ?>

                <div class="post-card">
                    <div class="post-card-inner">
                        <?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
                                    <a href="<?php the_permalink(); ?>" 
                                    rel="bookmark" 
                                    title="Permanent Link to <?php the_title_attribute(); ?>">

                                        <p><?php the_title(); ?></p>
                                    </a>
                    </div>

                </div>

                        <?php

                            // End the loop.
                            endwhile;
                             else: 
                        ?>
                                    <h1>No posts here!</h1>
                        <?php endif; 
                         wp_reset_postdata(); ?>
            </div>

This is the loop from single-journal.php:

 <?php $args = array('post_type' => 'journal', 
            'showposts' => 1,
            'post_status' => 'publish',
            'orderby' => 'date',
            'order' => 'DESC'); 
             $loop = new WP_Query($args); 
             if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

                <?php get_template_part( 'content', get_post_format() ); ?>

                <!--- Add authorname taxonomy -->
                <?php the_terms( $post->ID, 'authorname', 'Author: ', ', ', ' ' ); ?>


                <hr/>
                <nav class="nav-single">
                    <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
                    <span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentytwelve' ) . '</span> %title' ); ?></span>
                    <span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'twentytwelve' ) . '</span>' ); ?></span>
                </nav><!-- .nav-single -->

                <?php comments_template( '', true ); ?>

            <?php endwhile; endif;  wp_reset_postdata();// end of the loop. ?>

This is the custom post type:

/*
** Journal Entry
**
*/

function journal_entry() { 
    // creating (registering) the custom type 
    register_post_type( 'journal',
            array(
                'labels' => array(
                    'name' => __( 'Journal Entry' ),
                    'singular_name' => __( 'Journal Entry' ),
                    'add_new' => __( 'Add New Journal Entry' ),
                    'add_new_item' => __( 'Add New Journal Entry' ),
                    'edit_item' => __( 'Edit Journal Entry' ),
                    'new_item' => __( 'Add New Journal Entry' ),
                    'view_item' => __( 'View Journal Entry' ),
                    'search_items' => __( 'Search Journal Entry' ),
                    'not_found' => __( 'No Journal Entry found' ),
                    'not_found_in_trash' => __( 'No Journal Entry found in trash' ),
                    'menu_name' => _x( 'Journal Entry', 'journal' ),
                ),
                //'taxonomies' => array('category'),
                'public' => true,
                'exclude_from_search' => false,
                'publicly_queryable'  => true,
                'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
                'capability_type' => 'post',
                'rewrite' => array("slug" => "journal"), // Permalinks format
                'has_archive' => true,
                'query_var' => true,
                'menu_position' => 5,
                'menu_icon' => 'dashicons-book-alt', /* the icon for the custom post type menu */
                //'register_meta_box_cb' => 'add_team_members_metaboxes'
                //'yarpp_support' => true
            )
        ); /* end of register post type */

    /* this adds your post categories to your custom post type */
    //register_taxonomy_for_object_type( 'category', 'stories' );
    /* this adds your post tags to your custom post type */
    //register_taxonomy_for_object_type( 'post_tag', 'custom_post_faq' );

}

add_action( 'init', 'journal_entry');



function journal_taxonomy() {  
    register_taxonomy(  
        'journal_categories',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
        'journal',        //post type name
        array(  
            'hierarchical' => true,  
            'label' => 'Custom Categories',  //Display name
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'journal-categories', // This controls the base slug that will display before each term
                'with_front' => false // Don't display the category base before 
            )
        )  
    );  
} 

add_action( 'init', 'journal_taxonomy');
2

You shouldn't be using custom queries in these templates. The main query (using have_posts();, the_post(); etc. without $loop) is already set up with the correct posts for these templates.

Look at the custom query you're using on the single template:

$args = array('post_type' => 'journal', 
        'showposts' => 1,
        'post_status' => 'publish',
        'orderby' => 'date',
        'order' => 'DESC'); 
         $loop = new WP_Query($args);

That's a query for the latest journal post. So of that's what you're going to see when you use $loop->the_post(). Also, showposts is deprecated. Use posts_per_page instead.

So this:

if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

Needs to be:

if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  • Thank you for your reply and insight. Isnt there a function I'd have to add to Functions.php in order to get the custom post type to show up in the main query? – ardev Jun 28 '18 at 13:46
  • No. What's in the main query is determined by the URL. WordPress will automatically query the correct post type when you visit its URL. Then it will load the right template according to the Template Hierarchy – Jacob Peattie Jun 28 '18 at 13:57
1

The Main Query already contains the correct post, but you are creating a new query and asking for a single post ordered by date, which will always give you the latest post.

Remove your custom query code and just run the default loop:

if( have_posts() ) : while( have_posts() ) : the_post(); 
    // your template tags here
endwhile; endif;

This will output the correct post from the global $wp_query.

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.