WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination - WP_Query (10 posts)

  1. miguelsaddress
    Member
    Posted 1 year ago #

    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!

  2. miguelsaddress
    Member
    Posted 1 year ago #

    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();
    ?>
  3. keesiemeijer
    moderator
    Posted 1 year ago #

    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

  4. miguelsaddress
    Member
    Posted 1 year ago #

    :( it still doesnt work... i also tried that before...
    im going crazy with this issue hehehehe

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    On what theme template file are you trying this?

  6. miguelsaddress
    Member
    Posted 1 year ago #

    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 );

  7. miguelsaddress
    Member
    Posted 1 year ago #

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

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    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' );
  9. miguelsaddress
    Member
    Posted 1 year ago #

    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 :-D

  10. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you've got it resolved :-)

Topic Closed

This topic has been closed to new replies.

About this Topic