WordPress tax_query and pagination issue

I am creating a wordpress template to query my custom post type posts by using tax_query. It works fine and I get the correct posts in page #1, but I see the same posts as seen at page #1 when I click page #2. What did i missing here? Thanks in advance.

<?php

get_header();
?>

<h1><?php
the_title();
?></h1> <hr> <br>
<div id="content" class="three_fourth <?php
echo of_get_option('blog_sidebar_pos');
?>">
        <?php

?>
  <?php
$temp     = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$page     = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args     = array(
    'tax_query' => array(
        array(
            "taxonomy" => "wpdic-category",
            "field" => "slug",
            "terms" => "featured-charities",
            "numberposts" => 5,
            "paged" => $paged
        )
    )
);


$wp_query = new WP_Query($args);
?>
  <?php

if ($wp_query->have_posts()):
    while ($wp_query->have_posts()):
        $wp_query->the_post();
?>
    <article id="post-<?php
        the_ID();
?>" <?php
        post_class();
?>>
      <header>
        <h2><a href="<?php
        the_permalink();
?>" title="<?php
        the_title();
?>" rel="bookmark"><?php
        the_title();
?></a></h2>

        <?php
        $post_meta = of_get_option('post_meta');
?>
        <?php
        if ($post_meta == 'true' || $post_meta == '')
          {
?>
          <div class="post-meta">
            <div class="fleft"></div>
            <div class="fright"></div>
          </div><!--.post-meta-->
        <?php
          }
?>      
      </header>
      <?php
        $post_image_size = of_get_option('post_image_size');
?>
      <?php
        if ($post_image_size == '' || $post_image_size == 'normal')
          {
?>
        <?php
            if (has_post_thumbnail())
              {
                echo '<a href="';
                the_permalink();
                echo '">';
                echo '<div class="featured-thumbnail"><div class="img-wrap">';
                the_post_thumbnail();
                echo '</div></div>';
                echo '</a>';
              }
?>
      <?php
          }
        else
          {
?>
        <?php
            if (has_post_thumbnail())
              {
                echo '<a href="';
                the_permalink();
                echo '">';
                echo '<div class="featured-thumbnail large"><div class="img-wrap"><div class="f-thumb-wrap">';
                the_post_thumbnail('post-thumbnail-xl');
                echo '</div></div></div>';
                echo '</a>';
              }
?>
      <?php
          }
?>

      <div class="post-content">

        <?php
        $post_excerpt = of_get_option('post_excerpt');
?>
        <?php
        if ($post_excerpt == 'true' || $post_excerpt == '')
          {
?>
          <div class="excerpt"><?php
            $excerpt = get_the_excerpt();
            echo my_string_limit_words($excerpt, 52);
?></div>
        <?php
          }
?>
        <div id="widget_my_contentwidget"><ul><li><a class="link" href="<?php
        the_permalink();
?>">  read more</a></li></ul></div>
<hr>
      </div>
    </article>

  <?php
    endwhile;
else:
?>
    <div class="no-results">
      <p><strong>There has been an error.</strong></p>
      <p>We apologize for any inconvenience, please <a href="<?php
    bloginfo('url');
?>/" title="<?php
    bloginfo('description');
?>">return to the home page</a> or use the search form below.</p>
      <?php
    get_search_form();
?> <!-- outputs the default WordPress search form-->
    </div><!--noResults-->
  <?php
endif;
?>

  <?php
if (function_exists('wp_pagenavi')):
?>
        <?php
    wp_pagenavi();
?>
    <?php
else:
?>
    <?php
    if ($wp_query->max_num_pages > 1):
?>
      <nav class="oldernewer">
        <div class="older">
          <?php
        next_posts_link('&laquo; Older Entries');
?>
        </div><!--.older-->
        <div class="newer">
          <?php
        previous_posts_link('Newer Entries &raquo;');
?>
        </div><!--.newer-->
      </nav><!--.oldernewer-->
    <?php
    endif;
?>
  <?php
endif;
?>
  <!-- Page navigation -->

  <?php
$wp_query = null;
$wp_query = $temp;
?>

<div id="footer">
<div class="clearfix">
  <?php
if (!dynamic_sidebar('Footer Content')):
?>
      <?php
endif;
?></div></div>

</div><!--#content-->



<?php
get_footer();
?>

Related posts

Leave a Reply

2 comments

  1. wp_pagenavi() will work with the global $wp_query by default; if you want to paginate some custom query you need to pass it as a parameter, like:

    wp_pagenavi( array( 'query' => $mycustomquery ) );
    
  2. I have encountered the same problem before. For some reasons i cannot make WP query to work, but instead i used query_posts. Here is the code

    <?php
    
    /**
     * @author MESMERiZE
     * @copyright 2012
     */
    $page     = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args     = array(
        'tax_query' => array(
            array(
                "taxonomy" => "wpdic-category",
                "field" => "slug",
                "terms" => "featured-charities",
                "numberposts" => 5,
                "paged" => $paged
            )
        )
    );
    query_posts($args);
    while(have_posts()): the_post();
    
    endwhile;
    ?>
    

    Then i added the wp pagenavi function after the endwhile keyword.

    if (function_exists('wp_pagenavi')) {
    wp_pagenavi();}