Wordpress Pagination on Post page paginate (the_posts_pagination) - wordpress

So basically I have some posts on a post page. The posts show and the pagination shows. However, when you click the next page it does not go to the next page.. it cleans the URL and just reloads the same page. Not sure if there is a different way to be doing this? Any advice here would be great!
Code:
<div class="list-grid-view">
<?php get_template_part( 'template-parts/episode-filter' ); ?>
<div class="filtered-posts">
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
global $wp_query;
$original_query = $wp_query;
$args = array(
'post_type' => 'episode',
'posts_per_page' => 8,
'paged' => $paged,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
);
$the_query = new WP_Query( $args );
$wp_query = $the_query;
if ( $the_query->have_posts()) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( 'template-parts/episode-content-post' );
endwhile;
the_posts_pagination(array(
'mid_size' => 2,
'prev_text' => __( '<', 'textdomain' ),
'next_text' => __( '>', 'textdomain' ),
));
endif;
$wp_query = $original_query;
?>
</div>
</div>

Related

Wordpress posts loop pagination - first page return 125 posts instead of 10 and the rest return 10

I'm trying to show 10 posts per page with pagination on WordPress and the first page return 125 posts instead of 10 and the rest of the pages return 10 posts as requested, please assist :)
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$output = array();
global $post;
$args = array('nopaging' => false, 'paged' => $paged, 'posts_per_page' => 10, 'post_type' => 'post', 'order'=> 'DES', 'orderby' => 'date');
$postslist = new WP_Query( $args );
if ( $postslist->have_posts() ) :
while ( $postslist->have_posts() ) : $postslist->the_post();
array_push($output, array("timestamp" => get_the_date('U'),"img_url" => get_the_post_thumbnail_url(), "title" => get_the_title(), "text" => get_the_content()));
endwhile;
wp_reset_postdata();
endif;
Here is the sample code which you can use for the pagination:
<?php
/**
* Looping through the content
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query (array(
'post_type' => 'custom_portfolio',
'orderby' => 'post_id',
'posts_per_page' => 10,
'paged' => $paged,
'order' => 'DESC'
)); ?>
<?php while ($loop -> have_posts()) : $loop -> the_post(); ?>
<?php endwhile; ?><?php wp_reset_query(); ?><?php wp_reset_postdata(); ?>
And here is the part for the pagination:
<!-- Pagination -->
<div class="pagination">
<?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', __( 'Previous Page', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next Page', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
Here is a good start for you, just adopt as you want.
The sticky posts was the problem, I have excluded the sticky_posts from the query, and it fixed the problem
'ignore_sticky_posts' => 1

Get all Posts If has same custom field values in Posts

Trying to get all the posts having the same zipcode as metavalue. Thanks in advance for the help.
<?php
$query = new WP_Query( array(
'post_type'=> array('service'),
'posts_per_page' => -1,
'meta_query' => array( array(
'key'=> 'zipcode',
'value'=> ','.$zip.',',
'compare'=> 'LIKE'
) )
));
?>
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
zipcode are numbers for example 12345. If posts have value 12345 in the custom field. then it should display all posts which have the 12345 value. The above code is working fine but displays only one post.
Following code will be the proper for the meta query.
$query_args = array(
'post_type' => 'service',
'posts_per_page' => -1,
'meta_query' => array(
array(
'value' => $zip,
'compare' => 'LIKE',
'key' => 'zipcode',
),
)
);
$query = new WP_Query($query_args);
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
Hope it helps.
This does the job for me:
$popularCourses = new WP_Query(
array(
'post_type' => 'courses',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'course-is-promoted',
'meta_value' => 'Yes',
'orderby' => 'date',
'order' => 'DESC'
)
);
There are two ways.
After watching this code i will suggest you just visit this link for your better understanding.
(1)
$args = array(
'meta_query' => array(
array(
'key' => 'Your_key',//Enter your meta key here
'value' => 'professionnel',//Enter you meta value
'compare' => '=',//Comparison type (option filed) .
)
)
);
$query = new WP_Query($args);
(2)
$output_loop = get_posts( array(
'meta_key' => 'Your_key',//Meta key
'meta_value' => 'Your_value',//Meta value
) );
Now just print_r($output_loop) for the better understanding.

How to create a bxslider with wordpress post type categories

I have a taxonomy called portfolio_cat with its categories.So now i need to create a slider with that categories as a title and their post items.How i can do that? What Wordpress loop i need to have so i could put in a slider Wordpress categories with their posts?
I dont know how to customize this loop to fit in
<?php
$query = new WP_Query( array('post_type' => 'portfolio', 'posts_per_page' => 7, 'order' => ASC ) );
while ( $query->have_posts() ) : $query->the_post();
?>
I think you need this:
$portfolioArgs = array(
'order' => 'ASC',
'post_type' => 'portfolio',
'posts_per_page' => 7,
'tax_query' => array(
array(
'taxonomy' => 'portfolio_cat',
'field' => 'slug'
)
)
);
$query = new WP_Query( $portfolioArgs );
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="slide">
<?php the_title( '<h2>', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php
endwhile;
?>

Wordpress wp_query custom post type query not working on second page

Using below code i'm listing all posts under custom post type people
<?php $loop = new WP_Query(array('post_type' => 'people', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'wpcf-people-sort-order','posts_per_page' => 4, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
); ?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; ?>
Below is my pagination,
<div class="cus-pagi">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
?>
</div>
Also i created a plugin using above code to display list of titles in sidebar. So whenever i access mysite.com/people both(i.e list of custom posts with pagination & sidebar list of post title) of my custom query are working fine.
If i go to second page, sidebar is showing empty.
did anyone know where i'm going wrong ?
You you need to use the Loop with your custom query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'people',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'wpcf-people-sort-order',
'posts_per_page' => 4,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; endif; wp_reset_postdata(); ?>

Wordpress custom query missing two posts

I am running a custom query but when returning posts_per_page, it is always two behind. I am using a custom post type called events, that is pulling in the code to display the posts from the functions file. I also want to implement pagination(not working also), will this be a problem?
I have ran var dump which returns everything in the query including posts before the current date. I imagine because it's dumping before the if statement? Any ideas?
Any ideas?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//Current exhibitions
$args = array(
'post_type' => 'exhibitions',
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
$exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
//Current
if ($exhibition_start_date >= date('Y-m-d') ) {
get_exhibition_container();
}
endwhile;
wp_reset_postdata();
?>
New code: (I've also added in my initial loop which brings in the page content, thought it could interfear?)
<div role="main" class="clearfix">
<div class="one-column">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//Current exhibitions
$args = array(
'post_type' => 'exhibitions',
'meta_query' => array(
array (
'key' => 'exhibition_start_date',
'value' => date('Y-m-d'),
'compare' => '>='
)
),
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 10,
'paged' => $paged
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
$exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
//Current
get_exhibition_container();
endwhile;
wp_reset_postdata();
?>
<div class="previous-holder">
<?php previous_posts_link(); ?>
</div>
<div class="next-holder">
<?php next_posts_link(); ?>
</div>
Instead of using a if condition in your loop, you should use meta_query param :
$args = array(
'post_type' => 'exhibitions',
'meta_query' => array(
array(
'key' => 'exhibition_start_date',
'value' => date('Y-m-d'),
'compare' => '>='
)
),
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 6,
'paged' => $paged
);
EDIT : About pagination
previous_posts_link codex :
Prints a link to the previous set of posts within the current query.
You should try using previous_posts_link() and next_posts_link() before wp_reset_postdata()

Resources