I try to show my posts in my index page with post meta for this reason I make a wp query like this:
<?php $recent = new WP_Query(
array(
'posts_per_page' => 5,
'post_type' => 'post',
'meta_key' => 'post-filter-select',
'meta_value' => 'music'));
while($recent->have_posts()) : $recent->the_post();?>
It's great and work but for pagination I have big problem! It don't work!
may I customize my index page query? How can I do this customization with keeping my pagination work ?
Quick example:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 5,
'paged' => $paged,
'post_type' => 'page',
'meta_value' => 'music'
);
$recent = new WP_Query($args);
?>
<?php if ($recent->have_posts()) : while ($recent->have_posts()) : $recent->the_post(); ?>
// loop
<?php endwhile; endif; ?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $recent->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $recent->max_num_pages) ?></li>
</ul>
</nav>
<?php wp_reset_query(); ?>
Hope help you.
Related
I'm trying to take 12 posts per a page, but it displays only 4 posts.
Has anyone encountered this problem?
Current Code
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'order' => 'DESC',
'paged' => $paged,
);
$lists = new WP_Query($args);
echo $lists->post_count; // this outputs 12!
<ul>
<?php if ($lists->have_posts()) : ?>
<?php while ($lists->have_posts()) : $lists->the_post(); ?>
<li>
<div>
<?php the_title(); ?>
</div>
</li>
<?php endwhile; ?>
<?php endif;
wp_reset_postdata(); ?>
</ul>
Please add wp_reset_query(); above $lists = new WP_Query($args);
wp_reset_query();
$lists = new WP_Query($args);
I'm having an issue trying to get pagination to work on the homepage of a site I'm working on.
The "Older" and "Newer" links show correctly, and it updates the url to reflect the page number but the posts content remains the same as it does on the first page when flipping through the pages. Here is the code I'm using, simplified of course:
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }
$get_featured_posts = new WP_Query( array(
'posts_per_page' => 9,
'post_type' => 'post',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'paged' => $paged,
'offset' => 5
) );
while( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
<h3 class="entry-title entry-added">
<?php the_title(); ?>
</h3>
<p class="short_description"><?php echo short_description('...', 16); ?> </em></p>
<p class="read_more">Read More</p>
<?php
endwhile;
?>
<?php
// Reset Post Data
wp_reset_query();
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
Can't seem to figure out what the problem is, any help would be appreciated.
try to use wp_reset_postdata(); instead of wp_reset_query(); and also updated condition for $paged.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$get_featured_posts = new WP_Query( array(
'posts_per_page' => 9,
'post_type' => 'post',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'paged' => $paged,
'offset' => 5
) );
while( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
<h3 class="entry-title entry-added">
<?php the_title(); ?>
</h3>
<p class="short_description"><?php echo short_description('...', 16); ?> </em></p>
<p class="read_more">Read More</p>
<?php
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
?>
Please read this and insert that code of pagination on your page and remove newer and older links
https://www.elegantthemes.com/blog/tips-tricks/how-to-add-pagination-to-wordpress
Figured out the problem thanks to #milo on the WordPress exchange. Turns out there are special considerations when using "offset" and pagination. Check this link for more details.
<?php
/* Template Name: Front page */
get_header();
?>
<div class="rfp_hide" id="rhm_show">
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'product',
'page' => $paged,
'posts_per_page' => 20,
'product_cat' => '',
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $post, $paged;
?>
<div class="rhm_container">
<?php echo do_shortcode('[show_post]'); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
</div>
</main><!--/main content -->
<?php get_footer(); ?>
So, this is my html5blank template that I have which shows posts.
I can't seem to be able to make the pagination work.
Well, that's not entirely true. I can see the "Next" button at the bottom. The href is example.com/page/2. However when I click it, it just refreshes the page and does not go to page 2.
Does anyone know why it might be like that?
Thank!
I have a section at the bottom of my wordpress page that should display a group of posts (custom post type), 9 at a time, with simple pagination to scroll to the next/prev 9.
This is my code so far:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => 'event',
'order' => 'ASC',
'posts_per_page' => 9,
'orderby'=> 'event_date',
'meta_key'=>'event_date',
'meta_query' => array(array('key'=>'event_date', 'value'=>date('Y-m-d'))),
'compare' => '>=',
'paged'=>$paged
)) ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article class="post-<?php the_ID(); ?>">
<h2 class="post_title"><?php the_title(); ?></h2>
<h3 class="date_posted sub_text"><?php echo date('l, F jS', strtotime(get_field('event_date'))); ?></h3>
<?php html5wp_excerpt('events_page_listing'); ?>
<a class=lead_in href="<?php the_permalink() ?>"></a>
</article>
<?php endwhile; ?>
<div><?php previous_posts_link('« Previous') ?></div>
<div><?php next_posts_link('More »') ?></div>
I don't completely understand the paging process and I've taken this code from snippets found on some forums but it's not working. Currently, with this setup above, it's just showing two of the posts, with no pagination.
Can anyone point me in the right direction or identify what I'm missing?
Try the following : -
change this line :-
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
to
if (get_query_var('paged')):
$paged = get_query_var('paged');
elseif (get_query_var('page')):
$paged = get_query_var('page');
else:
$paged = 1;
endif;
My article on this http://codenathan.com/wordpress/pagination-on-wordpress-page
The 'compare' index should be inserted in the field array within the meta_query.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'event',
'order' => 'ASC',
'posts_per_page' => 9,
'orderby'=> 'event_date',
'meta_key'=>'event_date',
'meta_query' =>
array(
array(
'key'=>'event_date',
'value'=>date('Y-m-d'),
'compare' => '>='
)
),
'paged'=>$paged
)
);
Fore reference: WordPress Codex
Hope it helps : )
I found this excellent function to display all the posts listed under a specific custom taxonomy. It works great. Found here http://gilbert.pellegrom.me/wordpress-list-posts-by-taxonomy I tried a number of ideas, however unsuccessful, to try and paginate the returned data. I either get no data or it continues to display the entire list. Some of my taxonomies have over 10K posts associated. So pagination would seem logical.
What I want to do is; have the information that gets returned create pages of 'n' number of posts and make links for the other pages (1,2,...4,5 etc). Any help is greatly appreciated.
I tossed this in my functions file;
function list_posts_by_taxonomy( $post_type, $taxonomy, $get_terms_args = array(),
$wp_query_args = array() ){
$tax_terms = get_terms( $taxonomy, $get_terms_args );
if( $tax_terms ){
foreach( $tax_terms as $tax_term ){
$query_args = array(
'post_type' => $post_type,
"$taxonomy" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => true
);
$query_args = wp_parse_args( $wp_query_args, $query_args );
$my_query = new WP_Query( $query_args );
if( $my_query->have_posts() ) { ?>
<h2 id="<?php echo $tax_term->slug; ?>" class="title">
<?php echo $tax_term->name; ?></h2>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
}
wp_reset_query();
}
}
}
?>
And this code goes in the template, stuff whatever 'taxonomy' name and it displays the data. Another questions I was not sure about, if the pagination should go in the function or the template.
<div class="my_class">
<?php
list_posts_by_taxonomy( 'my_posttype', 'taxo_mytaxo' );
?>
</div>
Thank you everyone!
In order to have pagination with your query first of all you have to use the paged parameter.
To get some extra info check the wordpress codex for Pagination
Usually you get the pagination variable like this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
Then you pass it to the query by including it in the query arguments for your code:
$query_args = array(
'post_type' => $post_type,
"$taxonomy" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => true
'paged' => $paged //I've added it here
);
Then you'll have to build the pagination links something like(this will be done inside the loop):
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>