Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Why isn't my pagination working? WordPress

It shows the correct number of events (what my custom post type is called), but does not add the next, previous links. There are more than two events so it should do. What part of my code below, do I need to change so that these links are showing, and my pagination works?
This is for a category page - taxonomy-event-categories.php

My Code:


<?php get_header(); ?>

<div id="leftcolumntext">
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$EM_Category = em_get_category($term_id); ?>


<span style="font-size:0.2em">
<h2><?php echo $EM_Category->output('#_CATEGORYNAME'); ?></h2>
</span>

<?php echo $EM_Category->output('#_CATEGORYNOTES'); ?></p>
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $termst = $term->slug;
$current_date = date('Y-m-d');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'event',
'posts_per_page' => 2,
'paged' => $paged,
'meta_key' => '_event_start_date',
'orderby' => '_event_start_date',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'event-categories',
'field' => 'slug',
'terms' => $termst
)
),
'meta_query' => array(
array(
'key' => '_event_start_date',
'value' => $current_date,
'compare' => '>=',
'type' => 'DATE',
),
)
);

$query = new WP_Query( $args );
if($query ->have_posts()) :
while($query ->have_posts()) :
$query ->the_post();
?>
<h1>
<span class="taxonomy-event-excerpt" >
<?php $attachment_id = get_field('orange_image_');
echo wp_get_attachment_image( $attachment_id, 'orange_img' );
?>


<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
</span>
<br/>



<span class="taxonomy-event-excerpt" >
Date:
<?php $startdate = get_post_meta($id, '_event_start_date', true) ?> - <?php $enddate = get_post_meta($id, '_event_end_date', true) ?>
<?php $starttime= get_post_meta($id, '_event_start_time', true) ?> <?php $endtime = get_post_meta($id, '_event_end_time', true) ; ?>
<?php $starttimewithoutseconds = date("H:i ", strtotime($starttime));
$endtimewithoutseconds = date("H:i ", strtotime($endtime)); ?>
<?php

if ($startdate == $enddate) {
$new_format_start = date('F j, Y,',strtotime($startdate));
echo $new_format_start . ' Time: '. $starttimewithoutseconds . '-' . $endtimewithoutseconds;
} else {
$new_format_start = date('F j, Y,',strtotime($startdate));
$new_format_end = date('j/M/Y', strtotime($enddate));
echo $new_format_start;

echo ' Time: '. $starttimewithoutseconds . '-' . $endtimewithoutseconds;
}
echo '</span>';

?>
<span class="second-image-taxonomy">
<?php
$attachment_id = get_field('event_image_');
echo wp_get_attachment_image( $attachment_id, 'featured_image' );
?>
</span>

<span class="taxonomy-event-excerpt" >
<?php
$content = get_the_content();
$trimmed_content = wp_trim_words( $content, 60, '<a href="'. get_permalink() .'">...[ read more ]</a>' ); ?>
<p><?php echo $trimmed_content; ?></p>


<div class="buttommove">
<a href="<?php
echo get_permalink()?>" style=" -moz-box-shadow:inset 0px 1px 0px 0px #ed834e;
-webkit-box-shadow:inset 0px 1px 0px 0px #ed834e;
box-shadow:inset 0px 1px 0px 0px #ed834e;
background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ed834e), color-stop(1, #ed834e) );
background:-moz-linear-gradient( center top, #ed834e 5%, #ed834e 100% );
background-color:#ed834e;
-webkit-border-top-left-radius:0px;
-moz-border-radius-topleft:0px;
border-top-left-radius:0px;
-webkit-border-top-right-radius:0px;
-moz-border-radius-topright:0px;
border-top-right-radius:0px;
-webkit-border-bottom-right-radius:0px;
-moz-border-radius-bottomright:0px;
border-bottom-right-radius:0px;
-webkit-border-bottom-left-radius:0px;
-moz-border-radius-bottomleft:0px;
border-bottom-left-radius:0px;
text-indent:0px;
display:inline-block;
color:#fffffb;
font-family:Arial;
font-size:13px;
font-weight:bold;
font-style:normal;
height:35px;
line-height:35px;
width:111px;
text-decoration:none;
text-align:center;
text-shadow:1px 1px 0px #ed834e;

"> More Info / Book</a> </span> <br />
<br/> <br />
</div>

<?php
endwhile;
?>
<?php next_posts_link( 'Older posts' ); ?>
<?php previous_posts_link( 'Newer posts' ); ?>
<?php

else:
?> There are currently no events in this category.
<?php endif;?>
</div>
<div id="rightcolumntext">
<?php dynamic_sidebar( 'text-sidebar' );?>
</div>

<?php get_footer(); ?>

Answers (4)

2015-07-11

timDesain Nanang answers:

try to change custom query to the main query.

change this part:
$query = new WP_Query( $args );
if($query ->have_posts()) :
while($query ->have_posts()) :
$query ->the_post();

to:
query_posts( $args );
if(have_posts()) :
while(have_posts()) : the_post();

2015-07-11

Navjot Singh answers:

Try replacing

<?php next_posts_link( 'Older posts' ); ?>


with

<?php next_posts_link( 'Older Posts', $query->max_num_pages ); ?>


[[LINK href="https://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query"]]Source[[/LINK]]

2015-07-11

Shoeb mirza answers:

Please change your this code

$query = new WP_Query( $args );

if($query ->have_posts()) :

while($query ->have_posts()) :

$query ->the_post();


to

$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);



Add the following codes in function.php

function get_next_posts_link( $label = null, $max_page = 0 ) {
global $paged, $wp_query;

2015-07-12

Arnav Joy answers:

add this to your functions.php file

add_action('pre_get_posts', 'aj_filter_tax');

function aj_filter_tax( $query ){
if( $query->is_tax('event-categories') ):
$query->set('posts_per_page', 2);
endif;

return $query;

}