Pagination in wp_query - php

This may be an easy but I cannot get it figured out. I have searched for hours with no luck on my situation. I need to get pagination to work with my_query
<?php
$count = 0;
$id_suffix = 1;
$items_per_row = 4;
$quality = 90;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query( array( 'posts_per_page' => '4', 'post_type' => 'portfolio') );
$grid_class = 'grid_3';
$desired_width = 220;
$desired_height = 190;
$terms = get_terms( 'portfolio_categories' );
$count_terms = count( $terms );
?>
//some php code
<?php while ( $wp_query -> have_posts()) : $wp_query -> the_post(); //query the "portfolio" custom post type for portfolio items ?>
//some more php code
<?php endwhile;?>
<div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">«</span> Older posts', 'thematic')) ?></div>
<div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">»</span>', 'thematic')) ?></div>
</ul>
I got the pagination to display but when clicking on another page it shows the same portfolio items. Any help would be appreciated.
I got the pagination to show using wp-pagenavi but the same issue with not changing the items.

You are constructing the $paged variable but aren't using it.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query(
array(
'posts_per_page' => '4',
'post_type' => 'portfolio',
'paged' => $paged // this is the missing part
) );
WordPress uses the $wp_query variable name. You should probably use a different one.

Thank you #maiorano84 and #s_ha_dum for pointing me in the right direction. I wanted to update this post for anybody that runs into the problem. The problem I was having was making the pagination work on the a static front page. After reading in the codex I figured out my problem.
Instead of $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
I had to use
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
That one little mistake is what kept the pagination to work. so the final code looked like this.
<?php
$count = 0;
$id_suffix = 1;
$items_per_row = 4;
$quality = 90;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$my_query = new WP_Query( array(
'posts_per_page' => 8,
'post_type' => 'portfolio',
'paged' => $paged
) );
$grid_class = 'grid_3';
$desired_width = 220;
$desired_height = 190;
$terms = get_terms( 'portfolio_categories' );
$count_terms = count( $terms );
?>
//Some php code
<?php while ( $my_query -> have_posts()) : $my_query -> the_post(); //query the "portfolio" custom post type for portfolio items ?>
(Some more php code)
<?php endwhile;
wp_pagenavi(array( 'query' => $my_query ) ); ?>
</ul><!-- END .portfolio-gallery -->
?>

Related

WP Pagination Loop not working in custom theme

Pagination loop not working in custom theme Home page template. Shows same content in all pages.
You can also suggest me some other code to fix the issue.
<?php
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
// start loop
if (have_posts()) : while (have_posts()) : the_post();
// if you use the <!-- more --> in your posts.
global $more;
$more = 0;
?>
<div class="post">
<?php the_title(); ?>
</div>
<?php endwhile; ?>
<div class="navigation">
<?php next_posts_link('Next'); ?>
<?php previous_posts_link('Previous'); ?>
</div>
<?php else: ?>
<div><h2>Nothing found</h2><p>No posts found for that query</p></div>
<?php endif; ?>
If above answer wont work for you, use WP_Query instead of query_posts.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged
);
$the_query = new WP_Query( $args );
?>
If the pagination is broken on a static front page you have to add the "paged" parameter this way:
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }
query_posts() isn't meant to be used by plugins or themes. Use WP_Query instead. It accepts the same parameters as query_posts does. Be aware that neither of these methods is the most efficient way to alter the default query. In fact, either method can also be responsible for breaking pagination.
If your theme is using either of these methods to query the main loop, you can replace it with the preferred way, that is to say, hooking into 'pre_get_posts' and altering the main query by using is_main_query(). This way is faster and more reliable because the query for the main loop is altered before the posts are retrieved from the database.
For example, lets say your theme queries the main loop like this on your home page and the pagination is not working:
<?php
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
// start loop
if (have_posts()) : while (have_posts()) : the_post();
// if you use the <!-- more --> in your posts.
global $more;
$more = 0;
?>
<div class="post">
<?php the_title(); ?>
</div>
<?php endwhile; ?>
<div class="navigation">
<?php next_posts_link('Next'); ?>
<?php previous_posts_link('Previous'); ?>
</div>
<?php else: ?>
<div><h2>Nothing found</h2><p>No posts found for that query</p></div>
<?php endif; ?>
Remove the query_posts part from your code:
<?php
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
?>
And add the query for your home page back in your theme's functions.php file:
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 and category pages
if(is_home()){
$query->set('posts_per_page', 7);
}
if(is_category()){
$query->set('posts_per_page', 3);
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
You can use conditional tags to target the pages where we want to alter the query. Like,
// alter the query for the Movies category page
if(is_home() || is_front_page){
$query->set('posts_per_page', 7);
}
<?php
// Get current page and append to custom query parameters array
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Define custom query parameters
$args = array( 'posts_per_page' => 5, 'paged' => $paged );
$custom_query_args = array( $args );
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
?>
<div class="blog-post">
<?php the_post_thumbnail('post-thumbnail'); ?>
<?php endwhile;
endif;
// Reset postdata
wp_reset_postdata();
echo "<div class='blog-nav'>";
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $custom_query->max_num_pages
) );
// Reset main query object
echo "</div>";
$wp_query = NULL;
$wp_query = $temp_query;
?>

get posts of taxonomy parent?

Im new with custom post type - taxonomy wordpress.
I created a template page to show list of products in custome taxonomy but i dont know how exactly to do.
Example:
Category
SubCategory
Product A in Category and B in Category>SubCategory.I want to click Category will show only Product A and if i click SubCat will show only Product B.
Tks for helping!
<?php
$wp_query->get_queried_object_id();
$args = array(
'post_type' => array('san-pham'),
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 100,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
);
$eq_query = new WP_Query( $args );
$wp_query->get_queried_object_id();
$offset = 0;
$count = 0;
$eq_total_posts = $eq_query->found_posts - $offset;
if ($eq_query->have_posts()) : // The Loop
$eq_count = $args['paged'] * $args['posts_per_page'] - $args['posts_per_page']; // Count items
?>
<?php
while ($eq_query->have_posts()): $eq_query->the_post();
$eq_count++;
$count++;
?>
<?php
if ($count == 4) {
$p4 = 'four';
$count = 0;
} else { $p4 = ''; }
?>
<div <?php post_class($p4); ?> id="pro-cat">
....
</div>
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>

Adding Pagination to WordPress Page Templates

I built a wordpress theme from an existing theme (One Engine Theme). The site is launched and all seems to be working well except pagination. I have tried so many things but failed to get it to work. All the template pages are meant to paginate at some point but no one seem to work. Below is the looping code for the blog page template:
<?php
$newsposts = get_posts();
foreach($newsposts as $post) :
setup_postdata($post); ?>
//the html codes are added here
<?php endforeach; ?>
I want to make post per page to be 4 and also add the default wordpress pagination function:
<?php posts_nav_link(); ?>
The page template here is blog.php, you can see the page in action here: www.kayodeolusoji.net/blog
You help will be well appreciated!
Try this:
I add some 'paged' atrribute in $args wiht WP_Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page=4;
$args = array(
'paged' => $paged,
'posts_per_page'=>$posts_per_page
);
$newsposts = new WP_Query($args);
while($newsposts->have_posts()) :$newsposts->the_post();
echo $post->ID.'<br />';
endwhile;
wp_reset_query();
global $wp_query;
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $newsposts->max_num_pages
) );
I was able to fix after several trials. Here is what I did.
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=4'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
//the html codes are added here
<?php endwhile; ?>
<nav>
<?php //the pagination codes ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>

Wordpress Related Posts by TAG (Custom Query)

:)
So, I am trying to create a "related posts" thing using the TAGS of the post is being read at the moment. My theme -- which was custom designed -- have a related posts, but based on the CATEGORY.
I tried to changed it to tag, but absolutely no success (just a blank screen. ;D)
Can you help me with that? :)
query_posts("post_type=any&cat=13&posts_per_page=8");
global $wp_query;
$totalposts = $wp_query->found_posts;
$i = 1;
if (have_posts()):
while (have_posts()):
the_post();
include (TEMPLATEPATH . '/inc/categorias.php');
$thumb_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID, 'full'));
$class_name = (empty($thumb_image))?("empty_image"):("");
$bigpost = ($i % 9 == 0)?"big-post $catURL":"$catURL";
$terms = get_terms( 'notepad', array(
'orderby' => 'count',
'hide_empty' => 0
));
Ok, I did it. :)
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page'=>8,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
$totalposts = $wp_query->found_posts;
$i = 1;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
include (TEMPLATEPATH . '/inc/categorias.php');
$thumb_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID, 'full'));
$class_name = (empty($thumb_image))?("empty_image"):("");
$bigpost = ($i % 9 == 0)?"big-post $catURL":"$catURL";
And then, after the loop...
$i++;
endwhile;
}
wp_reset_query();
}

Previous / Next page link using wp_query loop

I am working on a wordpress site wich has a custom post type called, let's say 'plops'. I only want to use this custom post type in the website. So in the index.php, I am looping through those with a WP_Query, here is the code :
<?php
$args = array( 'post_type' => 'plops', 'posts_per_page' => 30, 'orderby' => 'desc');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$meta = get_post_meta($post->ID);
?>
// my template for the post...
<?php endwhile; ?>
The thing is I want to implement an infinite scroll to this page, and for that I need to have pagination links. I tried to implement those with the next_posts_link() function, but can't make it work !
It just won't display any link, and I tried a lot of stuff, nothing seems to make it work...
if i go to mysite.com/worpress/page/2, My posts do display, but I get a 404 in firebug... weird...
Any ideas ? Would really appreciate help ! Thanks a lot in advance !
The fix below might help you - this worked for me. It combines Chris Coyier's solution here with some help found in the comments. CSS Tricks Article
//Fix homepage pagination
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array('post_type' => 'custom_post_type', 'paged' => $paged );
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query( $args );
while($wp_query->have_posts()) : $wp_query->the_post();
?>
<!-- LOOP: Usual Post Template Stuff Here-->
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
It has been a long time since this question is posted, but for anyone who is looking for a way to achieve this in his/her own code:
$args = array( 'post_type' => 'plops', 'posts_per_page' => 30, 'order' => 'desc');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
$meta = get_post_meta($post->ID);
// my template for the post...
}
previous_posts_link('« Newer');
next_posts_link('Older »', $loop->max_num_pages);
wp_reset_postdata();
Note: You have to pass the max number of pages to the next_posts_link function only.

Categories