wp_query by last month + sort posts - wordpress

I'm using the WordPress plugin 'I recommend this', it allows people to 'like' posts.
The value is stored as a meta key, which we can query to generate a 'most recommended' page.
Plugin -> http://wordpress.org/support/plugin/i-recommend-this
The loop.
<?php
query_posts('&orderby=meta_value&meta_key=_recommended');
if (have_posts()): while (have_posts()) : the_post();
?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
</article>
<?php endwhile; ?>
<?php endif; ?>
This works, it finds the most recommended posts.
Two questions.
How do I limit the posts returned to a date range of say, the last 3 months?
In a similar vein, how could I then have a button for 'Most popular this week' that'd allow users to see a filtered result of posts from the last 7 days?

You could try something like this, this is just a basic example:
$today = getdate();
$threemonths = $today["mon"]-3;
$args = array(
'date_query' => array(
array(
'after' => array(
'year' => $today["year"],
'month' => $threemonths,
'day' => $today["mday"],
),
'before' => array(
'year' => $today["year"],
'month' => $today["mon"],
'day' => $today["mday"],
),
'inclusive' => true,
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
They break it down further in the wordpress documentation:
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

I worked it out. Used a custom filter along with WP_query array.
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-180 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$featuredPosts = new WP_Query( array(
'meta_key'=>'_recommended',
'orderby' => 'meta_value_num',
'order' => DESC
) );
remove_filter( 'posts_where', 'filter_where' ); ?>
<?php if ( $featuredPosts->have_posts() ) : ?>
<?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<div class="figure">
<?php the_post_thumbnail('frontthumb'); ?>
</div>
<div class="fig-cover">
<div class="fig-bottom">
<div class="title-container">
<?php if ( get_post_meta($post->ID, 'Price', true) ) { ?>
<div class="price-container">
<?php echo get_post_meta($post->ID, "Price", true); ?>
</div>
<h2 class="price-title"><?php the_title(); ?> </h2>
<?php } else { ?>
<h2><?php the_title(); ?> </h2>
<?php } ?>
</div> <!-- end div title-container -->
</div>
</div>
<div class="reco">
<?php if( function_exists('dot_irecommendthis') ) dot_irecommendthis(); ?>
</div>
<a class="the-post-link" href="<?php the_permalink(); ?> ">
</a>
</article> <!-- end div post -->
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
Now I just need to figure out how to do live filter on-site...

Related

Wp query pagination and categories

I'm new to Wp and I'm trying to developing my first theme. After I add the paginations to my archive page the category filter doesn't work anymore (probably because I override the WP Query).
What shoud I put in the query in order to see only the posts of the category that is clicked? (if I don't put anything wp do the magic) but without the query the pagination doesn't work.
This is the archive.php
<?php
get_header(); ?>
<?php
if ( have_posts() ) : ?>
<header class="page-header no-image">
<?php
the_archive_title( '<h1 class="page-title u-text-center">', '</h1>' );
?>
</header><!-- .page-header -->
<?php get_template_part( 'template-parts/filter-category' ); ?>
<?php
/*QUERY:
This is the problem if I move the query from there the category works but
If I leave the query the pagination doesn't have the post_per_page argument to take and doesn't work...
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page' => 6,
'paged' => $paged
);
$wp_query = new WP_Query( $args );
/*END QUERY*/
?>
<div id="blogpost-list" class="container">
<div class="row blogpost-wrapper">
<?php
/* Start the Loop */
$i = 1;
while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php if ($i % 3 == 0): ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post.. doesn't really matter-->
<?php get_template_part( 'template-parts/content-post-preview-large' ); ?>
</div>
<?php else: ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post doesn't really matter -->
<?php get_template_part( 'template-parts/content-post-preview' ); ?>
</div>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile;?>
</div>
</div>
<?php get_template_part( 'template-parts/pagination' ); ?>
<?php
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
<?php
get_footer();
Here the code that I use for the pagination:
<?php //Require a wp->query ?>
<div class="container-fluid">
<div class="pagination-wrapper">
<?php
$pag_args = array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $wp_query->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( '<img class="icn icn-small" src="' . get_template_directory_uri() .'/assets/images/icn-chevron-left.svg"/>'),
'next_text' => sprintf( '<img class="icn icn-small" src="' . get_template_directory_uri() .'/assets/images/icn-chevron-right.svg"/>'),
'add_args' => false,
'add_fragment' => '',
);
echo paginate_links($pag_args);
?>
</div>
</div>
SOLVED:
Add this to get the category
get_header();
$catID = get_queried_object_id();
Pass that to the query:
$args = array(
[...]
'cat' => $catID
);
$wp_query = new WP_Query( $args );
The problem is that you are not using your $wp_query variable after its instanciation.
Your while loop should look like this:
while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
/*Your inner code here*/
<?php endwhile;?>
You can create the category.php file if it' not exists. Then Inside your file
<?php
get_header();
$catID = get_queried_object_id();
?>
<?php
if ( have_posts() ) : ?>
<header class="page-header no-image">
<?php
the_archive_title( '<h1 class="page-title u-text-center">', '</h1>' );
?>
</header><!-- .page-header -->
<?php get_template_part( 'template-parts/filter-category' ); ?>
<?php
/*QUERY:
This is the problem if I move the query from there the category works but
If I leave the query the pagination doesn't have the post_per_page argument to take and doesn't work...
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page' => 6,
'cat' => $catID,
'paged' => $paged
);
$wp_query = new WP_Query( $args );
/*END QUERY*/
?>
<div id="blogpost-list" class="container">
<div class="row blogpost-wrapper">
<?php
/* Start the Loop */
$i = 1;
while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php if ($i % 3 == 0): ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post.. doesn't really matter-->
<?php get_template_part( 'template-parts/content-post-preview-large' ); ?>
</div>
<?php else: ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post doesn't really matter -->
<?php get_template_part( 'template-parts/content-post-preview' ); ?>
</div>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile;?>
</div>
</div>
<?php get_template_part( 'template-parts/pagination' ); ?>
<?php
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
<?php
get_footer();

How to re-use initial page content loop in WordPress?

I'm working on a site whereby there is a simple template for a certain page in the site.
The page in question is an About Us page.
On the page there is the first main WordPress loop which just spits out the standard content of the main Wysiwyg field.
Underneath that the template has some code which spits out a loop of a custom post type called Staff. This custom post type has information on each staff member and each one is spat out there.
What I have added to the page via the backend is an Advanced Custom Field using ACF Pro. The field is called Our Story (our_story) and I'd like to be able to spit that field out on the template underneath all the staff members.
If I place the following code :
<p><?php the_field('our_story') ?></p>
into the template in the main WordPress loop which is at the start of the template then it shows up fine but I effectively want to start the same loop again but underneath the loops which have spat out the staff members.
If I copy the exact same loop which is at the start of the template to the end of the template then I don't get anything output on the page where that code is. Do I therefore need to reset the code or something or is there an easy way of perhaps caching field content and placing it elsewhere in the template?
Sorry entire template code shown here :
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php the_field('our_story', '710'); ?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
I've adjusted your template to include named WP_Query() loops, and apply wp_reset_postdata() after each custom loop. This should allow the final the_field() call to have access to the current post ID, which is what Advanced custom fields needs in order to fetch the post metadata for the current post.
Behind the scenes, ACF runs (int) get_the_ID(); to get the ID of the current post. If your custom query loops are not reset, this won't work.
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
// Specify the custom query, so you can build a custom loop.
// ---------------------------------------------------------
$staff_query = new WP_Query( $args );
?>
<?php if ($staff_query->have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while ($staff_query->have_posts()): ?>
<?php
// Set up the post to allow use of template tags
// -------------------------------------------------
$staff_query->the_post();
?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$expert_query = new WP_Query( $args );
?>
<?php if ($expert_query->have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while ($expert_query->have_posts()): ?>
<?php
// Set up post
// -------------------------------------------------
$expert_query->the_post();
?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php
// this should work now:
the_field('our_story');
//the_field('our_story', '710');
?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
I think it's easy to get confused with custom loop logic when it's all handled within a template - I prefer to manage post data for custom loops by means of a function or class method which returns an array of post data - you can then loop over this in your template.
Hope this helps.
Resources: https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
it's because you need a post id to get the field. By default it is the current post's id.
try this: <p><?php the_field('our_story', xxx) ?></p> where xxx is the post id of About Us.

Prevent Post Duplication

I am in charge of managing this site F9 Properties which is built in WordPress. On the home page there is a featured properties section. I noticed that if you listed a property with two different "Status" such as "For Sale or For Lease, the property appeared twice in the carousel. Below is the code for listing the featured properties. I can see that it filters out the properties with the Status "Leased". Can anyone help me add a bit of code to list only one property per post regardless of how many different property status it has?
<?php
/* Featured Properties Query Arguments */
$featured_properties_args = array(
'post_type' => 'property',
'posts_per_page' => 100,
'meta_query' => array(
array(
'key' => 'REAL_HOMES_featured',
'value' => 1,
'compare' => '=',
'type' => 'NUMERIC'
)
)
);
$featured_properties_query = new WP_Query( $featured_properties_args );
if ( $featured_properties_query->have_posts() ) :
?>
<section class="featured-properties-carousel clearfix">
<?php
$featured_prop_title = get_option('theme_featured_prop_title');
$featured_prop_text = get_option('theme_featured_prop_text');
if(!empty($featured_prop_title)){
?>
<div class="narrative">
<h3><?php echo $featured_prop_title; ?></h3>
<?php
if(!empty($featured_prop_text)){
?><p><?php echo $featured_prop_text; ?></p><?php
}
?>
</div>
<?php
}
?>
<div class="carousel es-carousel-wrapper">
<div class="es-carousel">
<ul class="clearfix">
<?php
while ( $featured_properties_query->have_posts() ) :
$featured_properties_query->the_post();
?>
<?php
$status_terms = get_the_terms( $post->ID,"property-status" );
if(!empty( $status_terms )){
foreach( $status_terms as $status_term ){
if($status_term->name=="Leased"){}else{
?>
<li>
<figure>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php
the_post_thumbnail('property-thumb-image',array(
'alt' => get_the_title($post->ID),
'title' => get_the_title($post->ID)
));
?>
</a>
</figure>
<h4><?php the_title(); ?></h4>
<p><?php framework_excerpt(8); ?> <?php _e('Know More','framework'); ?> </p>
<span class="price"><?php property_price(); ?></span>
</li>
<?
}
}
}
?>
<?php
endwhile;
wp_reset_query();
?>
</ul>
</div>
</div>
I might be misunderstanding your setup, but I wonder why you're looping over the terms.
I think you should instead consider excluding the leased term within the WP_Query() part (hopefully you can share it).
Then your carousel would be simplified to:
<div class="carousel es-carousel-wrapper">
<div class="es-carousel">
<ul class="clearfix">
<?php while ( $featured_properties_query->have_posts() ) : $featured_properties_query->the_post(); ?>
<li><!-- YOUR POST ITEM HERE --></li>
<?php endwhile; ?>
</ul>
</div>
</div>
You can add the post ID to an array every time an iteration occurs, and check the array if the post has already been rendered:
$shown = array(); // new array
while ( $featured_properties_query->have_posts() ) :
$featured_properties_query->the_post();
$status_terms = get_the_terms( $post->ID, 'property-status' );
if( ! empty( $status_terms ) ){
foreach( $status_terms as $status_term ){
if( $status_term->name == "Leased" || in_array( $post->ID, $shown ){
continue; // post has term "Leased" or already rendered, skip
}
$shown[] = $post->ID; // add post ID to array
?>
<!-- HTML here -->
<?php
}
}
endwhile;

Display Posts Category Wise

I have code to show the latest 3 posts of every category and I want to exclude the child categories, but it doesn't exclude the sub category and it shows sub category separately and it show the latest 3 posts of sub category. Is there any way to exclude sub categories from loop.
Here is the code:
<?php
//start page loop
if (have_posts()) : while (have_posts()) : the_post();
//get meta to set parent category
$library_filter_parent = '';
$library_parent = get_post_meta($post->ID, 'wts_library_parent', true);
if($library_parent != 'select_library_parent') { $library_filter_parent = $library_parent; } else { $library_filter_parent = NULL; }
?>
<div id="library-by-category-wrap">
<?php
//get meta to set parent category
$library_filter_parent = '';
$library_parent = get_post_meta($post->ID, 'wts_library_parent', true);
if($library_parent != 'select_library_parent') { $library_filter_parent = $library_parent; } else { $library_filter_parent = NULL; };
//term loop
$terms = get_terms('library_cats','orderby=custom_sort&hide_empty=1&child_of='.$library_filter_parent.'');
foreach($terms as $term) { ?>
<div class="heading">
<h2><?php echo $term->name; ?></h2>
</div>
<div class="library-category">
<?php
//tax query
$tax_query = array(
array(
'taxonomy' => 'library_cats',
'terms' => $term->slug,
'field' => 'slug'
)
);
$term_post_args = array(
'post_type' => 'library',
'numberposts' => '3',
'tax_query' => $tax_query
);
$term_posts = get_posts($term_post_args);
//start loop
foreach ($term_posts as $post) : setup_postdata($post);
//get images
$featured_image = get_the_post_thumbnail($post->ID, 'cat-thumbnail'); ?>
<?php if(!empty($featured_image)) { ?>
<div class="library-item">
<a class="library-title" href="#" title="<?php the_title(); ?>" target="_blank">
<h3><?php the_title(); ?></h3>
</a>
</div>
<!-- /library-item -->
<?php } ?>
<?php endforeach; ?>
</div>
<!-- /library-category -->
<?php } wp_reset_postdata(); ?>
</div>
<!-- /library-by-category-wrap -->
<?php wp_reset_query(); ?>
<?php endwhile; endif; ?>
<?php
//get all terms (e.g. categories or post tags), then display all posts in each term
$taxonomy = 'category';// e.g. post_tag, category
$param_type = 'category__in'; // e.g. tag__in, category__in
$term_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts in '.$taxonomy .' '.$term->name;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Note see the Time Parameters in the query_posts() article.
<?php // get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h2>".$cat->name."</h2>";
// create a custom wordpress query
query_posts(“cat=$cat_id&post_per_page=100");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?>
<?php echo '<hr/>'; ?>
<?php endwhile; endif;
// done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
<?php
$catquery = new WP_Query( array( 'post_type' => 'testimonials', 'category_name'=>'hello','posts_per_page' => 10 ) );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li>
<h3>
<?php the_title(); ?>
</h3>
</li>
<li>
<?php the_content(); ?>
</li>
</ul>
<?php endwhile; ?>
$taxonomy = 'category';
$term_args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0
);
$terms = get_terms($taxonomy, $term_args);
[homepage_cat_grid limit='3' cat_id='2']
add_shortcode( 'homepage_cat_grid', array($this, 'homepage_postgrid_shortcode') );
/**
* HOME PAGE CATEGORY WISE POST GRID
*/
public function homepage_postgrid_shortcode( $atts, $content ) {
extract( shortcode_atts( array (
'order' => '',
'orderby' => '',
'limit' => '',
'cat_id'=> ''
), $atts ) );
ob_start();
$args = array(
'post_type' => 'post',
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $limit,
'cat' => $cat_id
);
$home_post = new WP_Query( $args );
if( $home_post->have_posts() ) : ?>
<div class="post-header">
<div class="head-left">
<?Php
$terms = wp_get_post_terms($home_post->post->ID,'category');
$cat_nm=$terms[0]->name;
if($cat_nm=='News'): echo "Recent News"; else: echo $cat_nm; endif;
?>
</div>
<a href="<?php
$category_link = get_category_link( $terms[0]->term_id );
echo $category_link; ?>" class="btn post-head-btn">VIEW ALL</a>
</div>
<div class="row">
<?php while ( $home_post->have_posts() ) : $home_post->the_post(); ?>
<div class="col-md-4">
<div class="single-grid">
<a href="<?php echo get_permalink( $post->ID ); ?>">
<?php the_post_thumbnail( $post->ID ); ?>
</a>
<div class="post-caption">
<?php echo the_category(); ?>
<div class="post-heading">
<?php echo the_title(); ?>
</div>
<div class="post-content">
<?php the_excerpt(); ?>
</div>
<div class="row">
<div class="col-12">
<a class="btn btn-dark cust-btn" href="<?php echo get_permalink( $post->ID ); ?>">Read More</a>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
<?php else :
get_template_part( 'template-parts/content', 'none' );
endif;
return ob_get_clean();
}

Adding pagination to custom post loop in page

I have created a custom page template (testimonials-page.php) and in that template I am
loading custom post type 'testimonials' using the following loop:
<?php query_posts(array(
'posts_per_page' => 5,
'post_type' => 'testimonials',
'orderby' => 'post_date',
'paged' => $paged
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
How do I add pagination to that? I installed the WP Paging plugin, and while that plugin works
great when I call the pagination into category.php using:
<p><?php wp_paging(); ?></p>
Inserting the same thing into testimonial-page.php results in broken formatting and links that
404 on me.
Firstly, never EVER use query_posts unless your intention is to modify the default Wordpress Loop.
Instead, switch to WP Query.
Here's something I wrote for a theme I did for a client using all built-in Wordpress functions. It's been working pretty well for me so far, so I'll integrate it into your code as best as I can:
global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'post_type' => 'testimonials',
'orderby' => 'post_date',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php
echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
the_content();
?>
</div>
<?php
endwhile;
echo '
<div id="wp_pagination">
<a class="first page button" href="'.get_pagenum_link(1).'">«</a>
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$query->max_num_pages;$i++)
echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
echo '
<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>
</div>
';
wp_reset_postdata();
endif;
?>
Jan 2018 Edit:
Also consider using paginate_links, since it's also built into Wordpress, and has more robust options and capabilities.
Try this code for custom loop with pagination:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source:
WordPress custom loop with pagination

Resources