Wordpress - List all posts by year with pagination - wordpress

Right now I'm using the following code to display all posts by year:
index.php
<?php foreach(posts_by_year() as $year => $posts) : ?>
<h2><?php echo $year; ?></h2>
<ul>
<?php foreach($posts as $post) : setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
functions.php
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish'
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date('Y', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
And right now I have set at the reading options in the admin panel that I only want to show 3 posts per page. With this new code, the posts by year, it displays all posts and in the pagination buttons there are 3 pages. When switching to another page, it displays the same content all over again.
How can I include the pagination now that I have posts by year? My pagination code is:
<?php if(function_exists('wpex_pagination')) { wpex_pagination(); } ?>
Which calls the function:
if ( !function_exists( 'wpex_pagination' ) ) {
function wpex_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
The idea is that I'm able to set a number of posts to be displayed. Not by year, but by posts. So if I have 30 from 2014 and 20 from 2013 and have the limit set to 5, it'll only show the first 5 from 2014.
Thanks in advance!
EDIT: Also found the following code, which works, but only on the first page. Meaning that in the first page it shows the date once, but in the following pages it shows the date for each post.
<?php
$date = 0;
$newDate = true;
if (have_posts()) : while (have_posts()) :
the_post();
if ($date == 0)
$date = the_date('Y');
else if ($date != the_date('Y')) {
$date = the_date('Y');
$newDate = true;
}
if ($newDate)
echo $date . ' ';
$newDate = false; ?>
<?php get_template_part('content'); ?>
<?php endwhile; endif; ?>

Related

List All posts under its own category title WP Query

Hi Hope You All doing well, I implemented a search for posts that are connected to categories child and then the parent of that child everything works perfect in search but I want to bring the search post under its own category and not to repeat that category title again and again for the post title here is my code script and check screenshot here what I mean to say
screenshot before search https://prnt.sc/10kzkvp
screenshot after search https://prnt.sc/10kzmxt As you can see here I want to list posts inside its own category and not repeat the category title for its post, let me know if need more explanation
<?php
$args = array(
'post_type' => 'post',
'extend_where' => "(post_title like '%$s_word%')",
'posts_per_page' => -1,
'orderby'=> 'title',
'order' => 'ASC'
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$icon_flag = false;
$child_c = "";
$parent_c = "";
while ( $the_query->have_posts() ) {
$the_query->the_post();
$split_title = explode(',', get_the_title(), 2);
$id = get_the_ID();
$current_cate = get_the_terms($id,'category');
$parent = $current_cate[0]->parent;
//load object for parent category
$parent_name = get_the_category_by_ID($parent);
if($parent_name != $parent_c && $parent != 0)
{
$parent_c = $parent_name;
?>
<h1 class="grand-p-title p-parent"><?php echo $parent_c; ?></h1>
<?php
}
if($current_cate[0]->name != $child_c )
{
$child_c = $current_cate[0]->name;
?>
<!-- <h1 class="grand-p-title p-child"><?php //echo $current_cate[0]->name; ?></h1> -->
<?php
}
?>
<p class="post-letters"><span class="first-w"><?php echo $split_title[0].", ";?></span><?php echo $split_title[1]; ?></p>
<?php
}
}else
{
?>
<p class="post-letters"><a>No posts founds</a></p>
<?php
}
wp_reset_query();
?>
I think you are doing wrong. you need to loop of categories first and then based on the category you have to get posts. for fetch all categories you can use WP get_terms()
<?php
$terms = get_terms( array (
'taxonomy' => 'category',
'hide_empty' => false
) );
// run a query for each term
foreach( $terms as $term ) {
// Define the query
$args = array(
'post_type' => 'post',
'cat' => $term->term_id,
'extend_where' => "(post_title like '%$s_word%')",
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
);
// run the query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) {
$icon_flag = false;
$child_c = "";
$parent_c = "";
while ( $the_query->have_posts() ) { $the_query->the_post();
$split_title = explode(',', get_the_title(), 2);
$id = get_the_ID();
$current_cate = get_the_terms($id,'category');
$parent = $current_cate[0]->parent;
//load object for parent category
$parent_name = get_the_category_by_ID($parent);
if($parent_name != $parent_c && $parent != 0) {
$parent_c = $parent_name; ?>
<h1 class="grand-p-title p-parent"><?php echo $parent_c; ?></h1>
<?php }
if($current_cate[0]->name != $child_c ) {
$child_c = $current_cate[0]->name; ?>
<!-- <h1 class="grand-p-title p-child"><?php //echo $current_cate[0]->name; ?></h1> -->
<?php } ?>
<p class="post-letters"><span class="first-w"><?php echo $split_title[0].", ";?></span><?php echo $split_title[1]; ?></p>
<?php }
}else { ?>
<p class="post-letters"><a>No posts founds</a></p>
<?php } wp_reset_query();
}

Counting the posts of a loop (WP_Query)?

I tried this way to display NO of post:
<?php
$news_2 = new WP_Query( array (
'post_type'=> 'jobs',
'posts_per_page'=> '10',
'meta_key' => 'status_for_jobs',
'meta_value' => '1'
) );
if ( $news_2->have_posts() ) {
while ( $news_2->have_posts() ) {
$news_2->the_post();
$count = $news_2->post_count;
?>
<li><h3><?php the_title(); ?></h3></li>
<?php
}
}
wp_reset_query();
?>
if the NO of post = 0 i need to display this :-
<?php
$news_2 = new WP_Query( array (
'post_type'=> 'jobs',
'posts_per_page'=> '10',
'meta_key' => 'status_for_jobs',
'meta_value' => '1'
) );
if ( $news_2->have_posts() ) {
while ( $news_2->have_posts() ) {
$news_2->the_post();
$count = $news_2->post_count;
if ($count == '0') {
?>
<li><h3>No Post</h3></li>
<?php
} else {
?>
<li><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a</h3></li>
<?php } ?>
<?php } } ?> <?php wp_reset_query(); ?>
But instead of the total of posts, I not getting any thing.
Any suggestions to fix this?
It sounds like what you wanted to use was $news_2->found_posts instead of $news_2->post_count.

Wordpress taxonomy loop pagination - Older post link not showing

I have the following loop, which is suppose to pull all taxonomy called series and the post under it. It works fine but the problem is it is not not showing Older posts link. I manually go to second page Newer post links shows up. Any idea what am I missing?
<?php
$page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
$per_page = 5;
$offset = ( $page-1 ) * $per_page;
$args = array( 'number' => $per_page, 'offset' => $offset, 'hide_empty' => 0,'paged' => $page);
$terms = get_terms('series',$args);
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'series','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo "<h3 class=\"term-heading\" id=\"".$term->slug."\">";
echo $term->name;
echo "</h3>";
if ($article_count) {
echo "<ul>";
while ($myquery->have_posts()) : $myquery->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
echo "</ul>";
} } ?>
<div class="clear"></div>
<p class="previous"><?php next_posts_link( __( '← Older posts', 'ari' ) ); ?></p>
<p class="next"><?php previous_posts_link( __( 'Newer posts →', 'ari' ) ); ?></p>
</div>
try this (I've tested), paged is not working good with offset, which is better for get_terms() (but still useful to get_query_var('paged')), also change "category" to your desired term.
<?php
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = 4;
$offset = ( $page-1 ) * $per_page;
$args = array( 'number' => $per_page, 'offset' => $offset, 'hide_empty' => 0);
$terms = get_terms('category', $args);
foreach ($terms as $term){
$wpq = array ('taxonomy'=>'category','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo "<h3 class=\"term-heading\" id=\"".$term->slug."\">";
echo $term->name;
echo "</h3>";
if ($article_count){
echo "<ul>";
while ($myquery->have_posts()) : $myquery->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
echo "</ul>";
}
} ?>

"Page not found" HTML title when I click the next link of wordpredd blog pagination

I have implemented a pagination links at the end of my wordpress blog page. In a one page I have 3 posts and when I click the next link of my pagination links it take me to the next page which contains the next 3 posts.
I retrieve posts from only one category. But when I go to the next page the html title of that page is "Page not found"
My code is like below:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('cat'=> 4, 'posts_per_page' => 3, 'paged' => $paged );
query_posts($args);
if(have_posts()) :
print ('<div class="row">');
while (have_posts()) : the_post();
$excerpt = get_the_excerpt();
//My post contents
endwhile;
if (function_exists("pagination")) {
pagination();
}
print ('<!-- end main row --></div>');
endif;
}
My pagination function is:
function pagination() {
/*
post: retun the pagination post cout and next previous buttons
*/
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages) {
$pages = 1;
}
}
if(1 != $pages) {
print('<section >');
if ($paged < $pages)
echo "NEXT";
if($paged <= $pages && $paged > 1)
echo "<a href='".get_pagenum_link($paged - 1)."'>BACK</a>";
echo "<p >Page ".$paged." of ".$pages."</p>";
echo "</section>";
}
}
Please help me for this
Try this insteatd
<?php
$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' => $the_query->max_num_pages
) );
?>
Try to add this
paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'total' => $total_pages,
'cat' => 4,
));
You missed category ID (cat=>4)
also change:
ADMIN PANEL -> SETTING -> READING -> Blog pages show at most : 3
as per $args = array('cat'=> 4, 'posts_per_page' => 3, 'paged' => $paged );

Wordpress custom wp_pagenavi does not work on custom page with custom query

I am trying to do some custom page and I am not using the wp_pagenavi plugin but a custom function with my custom pages, right now it only works on index.php pages but it was working fine some days ago before I've added more query elements.
//custom pagepavi function
function my_pagenavi( $the_query = false ){
global $wp_query;
$query = ($the_query) ? $the_query : $wp_query;
$max = $query->max_num_pages;
$current_page = max(1, get_query_var('paged'));
$big = 999999999;
if ( $max > 1 ) {
echo "<div class='pagination' style='height:auto'>";
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'show_all' => false,
'total' => $max,
'type' => 'list',
'prev_text' => __('PREV','dnp_theme'),
'next_text' => __('NEXT','dnp_theme'),
));
echo "</div>";
}
}
Now here is the loop from my template page.
//some query stuff
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = 'offset=0&paged='.$paged;
$blogs = new WP_Query($query);
if ( $blogs->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $blogs->have_posts() ) : $blogs->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php my_pagenavi( array('query' => $blogs) ); ?>
<?php endif; ?>
Why is not loading anything? What is going on??
The query is not correct that is why it's not working, at least it was not, not I have it working fine :)
instead of
$blogs = new WP_Query($query);
and all that should be
$blogs = query_posts( 'post_type=post&posts_per_page=4&paged='.get_query_var('paged') );
the loop is just like any other and works perfect
if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php my_pagenavi(); ?>
<?php endif; ?>
after endwhile write this code
global $wp_query;
$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' => $wp_query->max_num_pages
) );

Resources