• Resolved miguelsaddress

    (@miguelsaddress)


    Hello, i know this topic has been covered lots of times but, after searching a lot, i couldnt find the solution.

    I want to use pagination but when i go to xxx/page/2 etc, it gives a 404… i will attach a very simple version of my code (no HTML in the middle) to see if anybody can help me… it is a bit frustrating 🙁

    <?php
    	get_header();
    ?>
    <?php
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args = array(
    		'post_type' => 'post',
    		'cat' => 3,
    		'posts_per_page' => 2,
    	);
    	global $wp_query;
    	$query = http_build_query( $args ) . "&paged=".$paged;
    
    	$temp = $wp_query;
    	$wp_query= null;
    
    	$wp_query = new WP_Query();
    	$wp_query->query( $query );
    
    ?>
    
    		<?php if ($wp_query->have_posts()) : ?>
    
    			<?php $i = 1; $tags = array(); $post_ids = array(); ?>
    			<?php while ( $wp_query->have_posts()) : $wp_query->the_post();
    
    			// $wp_query->next_post();
    
    				$post = get_post( get_the_ID() );
    				$tags = array_merge( $tags, wp_get_post_tags( get_the_ID(), array( 'fields' => 'names' )  ));
    				$post_ids[] = get_the_ID();
    				$post_url = get_permalink( get_the_ID() );
    				?>
    				<strong>
    					<a href="<?php echo $post_url; ?>">
    						<?php the_title(); ?>
    					</a>
    				</strong>
    				<?php the_excerpt(); ?>
    			<?php endwhile; ?>
    
    			<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
    			  $wp_query = null; $wp_query = $temp;
    			?>
    
    		<?php endif;?>
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    any idea?

    this doesnt work in “normal” templates such as 2012 or similar… i also rebuilt permalinks.. etc.

    So far i am in localhost, developing

    thanks in advance!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter miguelsaddress

    (@miguelsaddress)

    i cleaned the code a lil bit

    <?php
    	get_header();
    
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args = array(
    		'post_type' => 'post',
    		'cat' => 3,
    		'posts_per_page' => 2,
    	);
    	global $wp_query;
    	$query = http_build_query( $args ) . "&paged=".$paged;
    
    	$temp = $wp_query;
    	$wp_query= null;
    
    	$wp_query = new WP_Query();
    	$wp_query->query( $query );
    
    	 if ($wp_query->have_posts()) :
    	 	$i = 1; $tags = array(); $post_ids = array();
    	 	while ( $wp_query->have_posts()) : $wp_query->the_post();
    
    				// $wp_query->next_post();
    
    			$post = get_post( get_the_ID() );
    			$tags = array_merge( $tags, wp_get_post_tags( get_the_ID(), array( 'fields' => 'names' )  ));
    			$post_ids[] = get_the_ID();
    			$post_url = get_permalink( get_the_ID() );
    
    			the_title();
    			the_excerpt();
    		endwhile;
    
    		if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
    		$wp_query = null; $wp_query = $temp;
    
    	endif; //have_posts
    
    	get_sidebar();
    	get_footer();
    ?>
    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args = array(
    		'post_type' => 'post',
    		'cat' => 3,
    		'posts_per_page' => 2,
    		'paged' => $paged
    	);
    
    	$temp = $wp_query;
    	$wp_query= null;
    
    	$wp_query = new WP_Query();
    	$wp_query->query( $args );

    If this is not on a Page request (page template) there are even better ways to query:

    http://www.billerickson.net/customize-the-wordpress-query/
    http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
    http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts

    Thread Starter miguelsaddress

    (@miguelsaddress)

    🙁 it still doesnt work… i also tried that before…
    im going crazy with this issue hehehehe

    Moderator keesiemeijer

    (@keesiemeijer)

    On what theme template file are you trying this?

    Thread Starter miguelsaddress

    (@miguelsaddress)

    it is a custom post type… it has its own archive template that i include in code

    public static function include_news_template_function( $template_path ) {
    	$cat = get_the_category();
    	$cat = $cat[0]->name;
    
    	if ( get_post_type() == 'post'  && $cat == 'NOTICIAS' ) {
    		if ( is_single() ) {
    			// checks if the file exists in the theme first,
    			// otherwise serve the file from the plugin
    			if ( $theme_file = locate_template( array( 'single-news.php' ) ) ) {
    				$template_path = $theme_file;
    			} else {
    				$template_path = plugin_dir_path( dirname(__FILE__ ) ) . 'theming/single-news.php';
    			}
    		}
    		if( is_archive() ){
    			if ( $theme_file = locate_template( array( 'archive-news.php' ) ) ) {
    				$template_path = $theme_file;
    			} else {
    				$template_path = plugin_dir_path( dirname(__FILE__ ) ) . 'theming/archive-news.php';
    			}
    		}
    	}
    	return $template_path;
    }

    and the filter is

    add_filter( ‘template_include’, array(‘News’, ‘include_news_template_function’ ), 1 );

    Thread Starter miguelsaddress

    (@miguelsaddress)

    sorry, it isnt custom_post_type in this case its a regular post, with a known category

    Moderator keesiemeijer

    (@keesiemeijer)

    So if see this correct, when you view a normal post category archive and the category is ‘NOTICIAS’ use the post type archive template in your theme or use a post type archive template from your plugin instead of a normal category template.

    Have you tried it without a query on the template (and a normal loop) with a query from your functions.php/plugin:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
      	// use category slug for is_category()
        if(is_category('noticias')){
          $query->set('posts_per_page', 2);
    
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    Thread Starter miguelsaddress

    (@miguelsaddress)

    dude,

    i think i am going to cry 😉 THAT WORKED!!

    I just inserted that code in my main plugin’s file… i can keep my own archive template… (normal loop style, and so on…removing the query i had)

    YOU ARE THE MAN, THANK YOU VERY MUCH… now lets see if it works everywhere i need it 😀

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you’ve got it resolved 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Pagination – WP_Query’ is closed to new replies.