How to get wp-pagenavi to work with wp query custom query wordpress

Had to get wordpress page navigation to work with a wp query i used the solution below

If you ar running a WP Query like below


wp query wordpress link

<?php

// The Query
$the_query = new WP_Query( 'posts_per_page=3' );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();

?>


You can then get the following to work with wp pagenavi with the code below


<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query('posts_per_page=3&paged=' . $paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>

<?php // the usual post-displaying codes here ?>

<?php
endwhile;
if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
$wp_query = null; $wp_query = $temp; ?>