WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] 404 error with custom post pagination (8 posts)

  1. adam.harvie
    Member
    Posted 1 year ago #

    Hi all,

    I've searched quite extensively for a solution to this, and none of the solutions seem to fix the problem. Hoping that I may be missing something that somebody will catch.

    I'm using Events Manager and the Structure theme. I'm trying to combine EM events (a custom post type) with regular posts on the homepage. Pagination fine for as many pages as a regular post appears on, but breaks as soon as there's a page with only events.

    I have tried turning off permalinks, toggling them off and on. I've made sure there's no Page called "event". I'm making sure there's a 'paged' variable. I've set the Settings > Reading post number to 1 (there's a separate option provided by the theme, seen below.)

    Here's the part of home.php that's setting up the query:

    <?php
    	        	// customizing arguments for WP_Query - altering the query so that both 'event' and regular post types are supported
    	        	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	        	$home_args = array(
    	        		 'post_type'=>array('post','event'),
     	        		'tax_query'=>array(
     	        			'relation' => 'OR',
     	        			array(
     	        				'taxonomy' => 'event-categories',
     	        				'field' => 'id',
     	        				'terms' => of_get_option('category_home_event'),
     	        			),
     	        			array(
     	        				'taxonomy' => 'category',
     	        				'field' => 'id',
     	        				'terms' => of_get_option('category_home'),
     	        			),
     	        		),
    
    	        		'posts_per_page' => of_get_option('postnumber_home'),
    	        		'paged' => $paged,
    	        	);
    			$wp_query = new WP_Query($home_args);

    Any help much appreciated!

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    If this is not a static front page (page template) you can query from your functions.php. Remove the whole new WP_Query from the loop: http://codex.wordpress.org/Pagination#Removing_query_posts_from_the_main_loop

    And query like this in your theme's functions.php:

    function my_post_queries( $query ) {
    	// do not alter the query on wp-admin pages and only alter it if it's the main query
    	if (!is_admin() && $query->is_main_query()){
    
    		// alter the query for the home page 
    
    		if(is_home()){
    
    			$query->set('posts_per_page', of_get_option('postnumber_home'));
    			$query->set('post_type', array('post','event'));
    
    			$tax_query = array(
    			  'relation'   => 'OR',
    			  array(
    			    'taxonomy' => 'event-categories',
    			    'field'    => 'id',
    			    'terms'    => of_get_option('category_home_event')
    			  ),
    			  array(
    			    'taxonomy' => 'category',
    			    'field'    => 'id',
    			    'terms'    => of_get_option('category_home')
    			  )
    			);
    
    			$query->set('tax_query', $tax_query);
    
    		}
    	}
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    I don't know if this will work with the event plugin though.

  3. adam.harvie
    Member
    Posted 1 year ago #

    Ah, ok, thanks. That fixes the pagination 404s, though there's another issue: the theme has a 'featured' posts area preceding the main paginated post list. It sets up the query for that in largely the same way:

    <?php
    	        	// customizing arguments for WP_Query - altering the query so that both 'event' and regular post types are supported
    	        	$featured_args = array(
    	        		'post_type'=>array('post','event'),
    	        		'tax_query'=>array(	//pull from both event and regular post categories
    	        			'relation' => 'OR',
    	        			array(
    	        				'taxonomy' => 'event-categories',
    	        				'field' => 'id',
    	        				'terms' => of_get_option('home_feature_event'),
    	        			),
    	        			array(
    	        				'taxonomy' => 'category',
    	        				'field' => 'id',
    	        				'terms' => of_get_option('home_feature'),
    	        			),
    	        		),
    	        		'posts_per_page' => 1,
    	        	);
    	        ?>
    				<?php $wp_query = new WP_Query($featured_args); ?>
                    <?php if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>

    If this first query is still present, then the main paginated one seems to share the query results, since I'm no longer doing new WP_Query($home_args); before the main query. Is there a way to set this up so that the first query doesn't get carried over?

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Maybe the example on this page will give you some idea.
    http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

    I'm sorry but I don't have much time right now.

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    I'm not sure this will work , but give it a try:

    function my_post_queries( $query ) {
    	// do not alter the query on wp-admin pages and only alter it if it's the main query
    	if (!is_admin() && $query->is_main_query()){
    
    		// alter the query for the home page 
    
    		if(is_home()){
    
    			$query->set('posts_per_page', of_get_option('postnumber_home'));
    			$query->set('post_type', array('post','event'));
    
    			$tax_query = array(
    			  'relation'   => 'OR',
    			  array(
    			    'taxonomy' => 'event-categories',
    			    'field'    => 'id',
    			    'terms'    => of_get_option('category_home_event')
    			  ),
    			  array(
    			    'taxonomy' => 'category',
    			    'field'    => 'id',
    			    'terms'    => of_get_option('category_home')
    			  ),
    			  array(
    			    'taxonomy' => 'event-categories',
    			    'field' => 'id',
    			    'terms' => of_get_option('home_feature_event'),
    			    'operator' => 'NOT IN'
    			  ),
    			  array(
    			    'taxonomy' => 'category',
    			    'field' => 'id',
    			    'terms' => of_get_option('home_feature'),
    			    'operator' => 'NOT IN'
    			  ),
    			);
    
    			$query->set('tax_query', $tax_query);
    
    		}
    	}
    }
    add_action( 'pre_get_posts', 'my_post_queries' );
  6. adam.harvie
    Member
    Posted 1 year ago #

    Ok I figured out a solution: It was key to move the modification of the main query out of the template (home.php) and into the function in functions.php with pre_get_posts. The other issue was solved by keeping the specific wp_query object to reference for the featured posts loop (which happens first in the template), and then using wp_reset_postdata():

    <?php
    	        	// customizing arguments for WP_Query - altering the query so that both 'event' and regular post types are supported
    	        	$featured_args = array(
    	        		'post_type'=>array('post','event'),
    	        		'tax_query'=>array(	//pull from both event and regular post categories
    	        			'relation' => 'OR',
    	        			array(
    	        				'taxonomy' => 'event-categories',
    	        				'field' => 'id',
    	        				'terms' => of_get_option('home_feature_event'),
    	        			),
    	        			array(
    	        				'taxonomy' => 'category',
    	        				'field' => 'id',
    	        				'terms' => of_get_option('home_feature'),
    	        			),
    	        		),
    	        		'posts_per_page' => 1,
    	        	);
    	        ?>
    				<?php $wp_query = new WP_Query($featured_args);
    				?>
                    <?php if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>
                    <?php $meta_box = get_post_custom($post->ID); $video = $meta_box['custom_meta_video'][0]; ?>
                    <?php global $more; $more = 0; ?>
    
                        <?php if ( $video ) : ?>
                            <div class="feature_video"><?php echo $video; ?></div>
                        <?php else: ?>
                            <a class="feature_img" href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail( 'home-feature' ); ?></a>
    
                        <?php endif; ?>
    
                        <div class="feature_info">
                            <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    
                        </div>
    
                    <?php endwhile; wp_reset_postdata(); ?>

    Then for the main loop, since the query modification was moved to functions.php, the specific wp_query object that was previously being used had to be removed, to make sure it's referencing the 'main' (global) object:

    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>

    Previously the query modification had all been in home.php, so that had been $wp_query->have_posts(), etc. Taking the query modification out and moving it in to functions.php meant there wasn't a $wp_query = new WP_Query() for the second loop, and the loop being referenced was the previous Featured posts one. Not being super familiar with WP yet, I didn't realize I could just take the object reference out to get the 'main' query.

    Many thanks to keesiemeijer. Your first bit of code and the link you provided put me on the right track.

  7. adam.harvie
    Member
    Posted 1 year ago #

    Forgot to say, the other place I got some helpful tips was from this stackexchange thread.

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    Well done! And thank you for that last link. I think I will add it to the troubleshooting pagination codex page.

    I'm glad you got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic