No numbered pagination custom post type - php

I have a problem showing numbered pagination on only one of my Wordpress pages.
<?php
/**
* Template Name: Sale template
*/
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array(
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => array(
'key' => 'prijsknaller',
'value' => '1',
'compare' => '==',
//'type' => 'date',
),
);
$query = new WP_Query($args);
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="product-overview">
<?php if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-md-3">
<div class="featured-block">
<div class="featured-block-image">
<a href="<?php the_permalink(); ?>"><img src="<?php the_field('productafbeelding'); ?>"
alt="<?php the_title(); ?>"></a>
</div>
<div class="featured-block-info">
<h4><?php the_title(); ?></h4>
<?php if (get_field('prijs_oud')) : ?>
<span class="oud"><?php the_field('prijs_oud'); ?></span>
<?php endif; ?>
<span class="nieuw"><?php the_field('prijs_nieuw'); ?></span> p/m<sub>2</sub>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php the_posts_pagination(array(
'prev_text' => '«',
'next_text' => '»',
)); ?>
</div>
</div>
</div>
</div>
</div>
The query returns about 70 products, so I would expect it to show at least 7 numbers. However, it does not show any pagination.

If you give your full code it will be more easier to find out your problem.
May be you use wp_reset_postdata(); before the_posts_pagination();
Please check this.

try this code it surely help you
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array (
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => array (
'key' => 'prijsknaller',
'value' => '1' ,
'compare' => '==',
//'type' => 'date',
),
);
$wp_query = new WP_Query( $args );

Replace your code with this:
<?php
/**
* Template Name: Sale template
*/
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($paged == "1") {
$args = array(
'post_type' => array(
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'offset' => 0,
'meta_query' => array(
'key' => 'prijsknaller',
'value' => '1',
'compare' => '==',
//'type' => 'date',
),
);
} else {
$offset = $paged * 5;
$offset = $offset - 5;
$args = array(
'post_type' => array(
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'offset' => $offset,
'meta_query' => array(
'key' => 'prijsknaller',
'value' => '1',
'compare' => '==',
//'type' => 'date',
),
);
}
$query = new WP_Query($args);
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="product-overview">
<?php if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-md-3">
<div class="featured-block">
<div class="featured-block-image">
<a href="<?php the_permalink(); ?>"><img src="<?php the_field('productafbeelding'); ?>"
alt="<?php the_title(); ?>"></a>
</div>
<div class="featured-block-info">
<h4><?php the_title(); ?></h4>
<?php if (get_field('prijs_oud')) : ?>
<span class="oud"><?php the_field('prijs_oud'); ?></span>
<span class="nieuw"><?php the_field('prijs_nieuw'); ?></span> p/m<sub>2</sub>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="pagination-grp">
<?php
$big = 999999999; // need an unlikely integer
//$i=1;
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'prev_text' => __('<'),
'next_text' => __('>'),
'total' => $loop->max_num_pages
));
wp_reset_postdata();
endif;
?>
</div>
</div>
</div>
</div>
</div>
</div>

Related

Posts per page not limiting posts in query

I have a query that spits out posts that aren't featured but for some reason the posts_per_page limiter isn't working...
<?php $featured_posts = get_posts( [
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'fields' => 'ids',
] );
query_posts( array( 'post__not_in' => $featured_posts ) );
while ( have_posts() ) : the_post();?>
<div class="news-item-block col-md-4" role="article">
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('news-grid-image'); ?>
</a>
<span class="news-item-date"><?php the_date( 'M d, Y' ); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php endwhile;
wp_reset_query();
?>
Thanks for any insight on this issue.
Yeah, use 'nopaging' => true and 'ignore_sticky_posts'=>true
<?php $featured_posts = get_posts( [
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'nopaging' => true,
'ignore_sticky_posts'=>true,
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'fields' => 'ids',
] );
query_posts( array( 'post__not_in' => $featured_posts ) );
while ( have_posts() ) : the_post();?>
<div class="news-item-block col-md-4" role="article">
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('news-grid-image'); ?>
</a>
<span class="news-item-date"><?php the_date( 'M d, Y' ); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php endwhile;
wp_reset_query();
?>
The problem you are facing here is that you are getting posts object in $featured_posts and thus calling query_posts is really unnecessary. From your code structure, I am assuming that you are trying to access the main query but with modified parameters. A better approach to accomplish your goal is as follows.
$args = array(
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'meta_compare' => '!=',
'fields' => 'ids',
);
$featured_posts = new WP_Query( $args );
while ( $featured_posts->have_posts() ) : $featured_posts->the_post();?>
<div class="news-item-block col-md-4" role="article">
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('news-grid-image'); ?>
</a>
<span class="news-item-date"><?php the_date( 'M d, Y' ); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php
endwhile;
wp_reset_query();
On a side note, I'd advise against using query_posts as it directly modifies the main query. But hopefully the above code should help you achieve desired results.
Got it. The trick was to target the while function e.g.:
<?php $i = 1; while (have_posts() && $i < 4) : the_post();?>
<?php the_post_thumbnail(); ?>
<?php the_date( 'M d, Y' ); ?>
<?php the_title(); ?>
<?php $i++; endwhile;?>

WordPress: posts_per_page not working on archive pages

I've a custom loop which should display 3 projects per category in a random order.
The problem is, that the loop always shows all projects in that category.
I've tested the the code on a single page and it works how it should and only showed 3 projects.
So I guess it has something to do with the archive page?!
Here's ist the loop itself:
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
And here's the full code (I guess not that relevant):
<?php
$args = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => 0 );
$categories = get_categories($args);
foreach($categories as $category) {
?>
<div class="row">
<div class="col-lg-4">
<?php
$term_child = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args_child = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => $category->term_id );
?>
<h4><?php echo $category->name; ?></h4>
</div>
<div class="col-lg-8">
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
<?php } ?>
Sorry, it was an extra function which sets the archive posts to 99.
Istead of is_post_type_archive I used is_archive

pagination doesn't work on a single page of WordPress

Pagination doesn't work on a single page of WP.
In the code below, everything looks great except the pagination shows the same contents on the next page.
I tried every possible solution I found on the internet, but none of them worked. I'm relatively new to WP and php, so if you could pinpoint the code that might be wrong, that'd be really helpful.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_cat',
'field' => 'slug',
'terms' => array( 'disney' )
),
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
I expect the pagination to work correctly instead of showing the same contents on all pages.
As you see, I want to make the pagination work only on the posts with "disney" slug.
You can replace the code instead of your old code
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
Now, you can check it that pagination is working or not.

How to add pagination for posts page if posts are getting dynamically by get_posts();

I'm working on a WordPress site, on it I've got a page with posts filtered by select option. Select option having different resource types. I've got 100 more posts for each resource type, but I want to display 10 posts per page, and the remaining should get pagination.
My code:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' =>10,
'offset' => 0,
'paged' => $paged,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
if(isset($args3) && !empty($cat_termId)){
$query = new WP_Query( array( 'category__and' => array($cat_termId) ) );
$posts = $query->posts;
}else{
$args1 = !empty($args2)?$args2:$args1;
$args = array_merge($args,$args1);
$posts = get_posts( $args );
}
endif;
if(!empty($posts) && !empty($cat_termId)){
echo '<div class="row mt-5 insight-page-data search-insights" id="sub-insights">';
foreach ($posts as $post){
//Post Details
$post_content = $post->post_content ;
$post_excerpt = wp_trim_words($post->post_excerpt, 10) ;
$post_name = $post->post_title ;
$name = $post->post_name ;
$link = get_the_permalink($post->ID);
$image =wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID),'full');
?>
<div class="col-sm-6 col-md-6 col-lg-4 my-3 wow fadeIn animated" data-wow-duration="1s" data-wow-delay=".3s">
<div class="card border-0 rounded-0 <?=$card?>" >
<div class="card-header border-0 py-2">
<img src="<?=$card_link?>" alt="" class="mr-2"><?= ucfirst($main_slug_name)?>
</div>
<div class="card-img-top card-img rounded-0 align-middle">
<img class="mx-auto d-block align-middle" src="<?php echo !empty($image[0])?$image[0]:$defaultImg?>" alt="MSR Cosmos <?= $main_slug_name?>"/>
</div>
<div class="card-body" id="alink1">
<h4 class="card-title"><?=$name?></h4>
<p class="card-text"><?=$post_excerpt?></p>
</div>
</div>
</div>
<?php }
echo '</div>';
} else{
echo "<p class='no-results'>No Insights Available</p>";
}
}
?>
Please help me to get the desired result. Am I really on the right path?
Use this code for adding pagination on the site.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$argument = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby'=>'date',
'order'=>'DESC',
'paged' => $paged
);
$loop = new WP_Query($argument); ?>
<ul class="products columns-4">
<?php
while ($loop->have_posts()) : $loop->the_post();
endwhile; ?>
</ul>
<div class="pagination" style="font-size: 25px !important;">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url(get_pagenum_link( 999999999 ) ) ),
'total' => $loop->max_num_pages,
'current' => max(
1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
/*'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', ( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', ( 'Older Posts', 'text-domain' ) ),*/
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>

PHP-code for displaying ACF-fields on Wordpress post?

I have a PHP-code snippet array showing a list of ACF-field values from posts. I would like to use that code on a single post and only show the ACF-values from the current post. So it´s no longer a list. I still need the date filtering.
What do I have to change for it to work like I intend?
<?php
$today = current_time('Ymd');
$args = array(
'post_type' => 'post',
'posts_per_page' => '20',
'meta_key' => 'kalenderdag',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'kalenderdag',
'compare' => '>=',
'value' => $today,
'type' => 'DATE'
),
),
);
$children = new WP_Query($args);
?>
<?php if ($children->have_posts()) : ?>
<?php while ($children->have_posts()) : $children->the_post(); $fields = (object) get_fields(); ?>
<div class="event row">
<div class="event-logo col-sm-4">
<?php the_post_thumbnail( 'large', array( 'class' => 'img-responsive' ) ); ?>
</div>
<div class="event-details col-sm-8">
<h2 class="underline"><a href="<?php the_permalink(); ?>"><?php echo $fields->kalendertitel; ?> - <?php $date = get_field('kalenderdag'); ?>
<?php echo date("d M Y", strtotime($date)); ?></a></h2>
<h3 class="underline"><?php the_title(); ?></h3>
<p><?php echo $fields->kalendertext; ?></p>
<p class="call-to-action">Läs mer</p>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<h2 class="page-title">Just nu har vi inga bokade evenemang</h2>
<p>
Kika gärna in på denna sida en annan gång eller kontakta oss på info#fredenshus.se om du har några frågor.
</p>
<?php endif; ?>
Can you just put the current ID in as an argument with 'p' => get_the_ID() to your WP_Query?
$args = array(
'p' => get_the_ID(),
'post_type' => 'post',
'posts_per_page' => '20',
'meta_key' => 'kalenderdag',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'kalenderdag',
'compare' => '>=',
'value' => $today,
'type' => 'DATE'
),
),
);

Categories