WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Loop with multiple post types not rendering archive pages (14 posts)

  1. jaledwith
    Member
    Posted 1 year ago #

    First off, the website I'm referring to is http://floridanavs.org/blog/.

    I'm creating a loop for this blog that includes multiple post types. All the following code is within home.php (rather than index.php).

    query_posts( array(
         		'post_type' => array( 'post', 'flnavs_podcast', 'flnavs_video', 'flnavs_resources' ) )
    	 		);
    			if ( have_posts() ) : $count = 0;
    				while ( have_posts() ) : the_post(); $count++;
    					get_template_part( 'loop', get_post_format() );
    				endwhile;
    			else: ?>
    				<div class="post">
    					<h2 class="title">
    						<?php _e('Sorry!','standardtheme'); ?>
    					</h2>
    					<p>
    						<?php _e('Whoops! Something broke. Please try again!','standardtheme'); ?>
    					</p>
    				</div>
    			<?php endif;
    			// Function that handles output of pagination
    			standard_pagination(); ?>

    This loop will successfully display the first 10 posts (as defined in the Reading panel in Settings) but when I click on Older Posts I get a 404 error. I've tried using WP_Query instead of query_posts but the same error occurs. Any idea what's going wrong?

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    Try replacing:

    query_posts( array(
         		'post_type' => array( 'post', 'flnavs_podcast', 'flnavs_video', 'flnavs_resources' ) )
    	 		);

    with:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'post_type' => array( 'post', 'flnavs_podcast', 'flnavs_video', 'flnavs_resources' ),
    	'paged' => $paged
    );
    query_posts($args);
  3. jaledwith
    Member
    Posted 1 year ago #

    Thanks for your reply. Unfortunately that didn't fix the problem. I should have mentioned that I'd tried that solution before. Perhaps it's needed in the final product but something else is missing too.

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Have you tried looking at the standard_pagination() function?

  5. jaledwith
    Member
    Posted 1 year ago #

    Yes. It looks pretty innocuous. It also performs just fine on other archive pages (category, author, etc.).

    function standard_pagination() { ?>
    		<div class="more_entries">
            	<div class="next-posts fl"><?php next_posts_link('&laquo; Older Posts') ?></div>
    		<div class="prev-posts fr"><?php previous_posts_link('Newer Posts &raquo;') ?></div>
    		</div>
    <?php } ?>
  6. esmi
    Forum Moderator
    Posted 1 year ago #

    What happens if you remove the custom query in home.php?

  7. jaledwith
    Member
    Posted 1 year ago #

    Well, there are only 9 posts in the default post type, but after adjusting the settings panel to display 5 posts per page both the blog and first archive page render properly.

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    So it sounds like there's a problem with your custom post types. Check that you have the correct names in that custom query

  9. jaledwith
    Member
    Posted 1 year ago #

    The names are correct. I used the $post_type parameter I defined for each custom post type. I've had no problem getting each post type to appear on the first page of the blog, so I know I've gotten the names correct all this time.

    I just stumbled across something really strange. Like I said, I altered the number of posts per page from 10 to 5. I tried going to the second page of the blog using those settings and it loaded successfully. But then I tried to go to the third page and I received a 404. Just to test things further I switched the posts per page from 5 to 3. I can load the first, second, and third pages of the blog, then I receive a 404 when I try to load the fourth. So, I can load no more than 10 posts, and it'll load fewer than that if the display setting doesn't divide evenly into ten.

    I'm completely baffled now.

  10. esmi
    Forum Moderator
    Posted 1 year ago #

    Have you tried:

    - deactivating all plugins to see if this resolves the problem. If this works, re-activate the plugins one by one until you find the problematic plugin(s).

    - resetting the plugins folder by FTP or PhpMyAdmin. Sometimes, an apparently inactive plugin can still cause problems.

  11. jaledwith
    Member
    Posted 1 year ago #

    Unfortunately, after trying both your suggestions I'm still seeing the same problem.

  12. racer x
    Member
    Posted 1 year ago #

    Out of curiosity, what happens if you place the number of posts right into the query rather than using the settings?

    'posts_per_page' => 3

    I have seen other similar posts here and that seemed to work.

  13. jaledwith
    Member
    Posted 1 year ago #

    Thanks for replying, racer x.

    Unfortunately that did not fix the issue I'm having, but it is responding differently than before. No matter what I set posts_per_page to I'm able to load the first three pages of posts before returning a 404.

  14. jaledwith
    Member
    Posted 1 year ago #

    Okay, out of sheer frustration I decided to go about the problem an entirely different way. I scrapped everything I'd changed in home.php and used the default loop. Then, in my functionality plugin, I added the following:

    function my_get_posts( $query ) {
    	if ( is_home() && $query->is_main_query() )
    		$query->set( 'post_type', array( 'post', 'flnavs_podcast', 'flnavs_video', 'flnavs_resources' ) );
    	return $query;
    }
    add_filter( 'pre_get_posts', 'my_get_posts' );

    It achieves the desired results. Thanks to all who helped me try to figure this mess out.

Topic Closed

This topic has been closed to new replies.

About this Topic