Support » Developing with WordPress » Pagination not working with wp_reset_postdata

  • Hello,
    I am using the latest WordPress and All in One Seo, my pagination is working fine. After some updates of All in One SEO, my titles are gone, after I change the code with wp_reset_postdata and now my pagination is gone. I do lots of searches but can’t figure it out. Here is the code.

    <?php $count = 1; ?>
    
    <?php 
    $temp = $sec_query;
    $sec_query= null;
    $sec_query = new WP_Query();
    $args =  get_post_meta($post->ID, 'job_tag', true);
    $sec_query->query('tag='.$args.'&posts_per_page=12&paged='.$paged);
    ?>
    
     
    <?php if ( $sec_query->have_posts() ) : ?>
     
    
    <?php while ( $sec_query->have_posts() ) : $sec_query->the_post(); ?>
     
    <?php get_template_part('includes/job-loop'); ?>
    
    		<!--Injecting Code-->
    			<?php if ($count == 6) : ?>
    				<div style="padding: 10px;">
    				<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    					<!-- JA Footer Responsive -->
    					<ins class="adsbygoogle"
         					style="display:block"
         					data-ad-client="PUB-ID"
         					data-ad-slot="4525903855"
         					data-ad-format="auto"></ins>
    				<script>
    				(adsbygoogle = window.adsbygoogle || []).push({});
    				</script>
    				</div>
    			<?php endif; $count++; ?>
    		<!--Injecting Code-->
    
    <?php endwhile; ?>
     
    <?php wp_reset_postdata(); ?>
     
    <?php else: ?>
     
    <?php _e( 'Sorry, no posts matched your criteria.' ); ?>
     
    <?php endif; ?>

    And this is my Pagination after this code..
    <?php if (function_exists('ja_pagination')) ja_pagination(); ?>

    Can you please help me to fix this issue?

    • This topic was modified 3 months, 1 week ago by Xaibi Aslam.
    • This topic was modified 3 months, 1 week ago by Xaibi Aslam.
    • This topic was modified 3 months, 1 week ago by Xaibi Aslam.

    The page I need help with: [log in to see the link]

Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator bcworkz

    (@bcworkz)

    You should setup the query like this:

    <?php 
    $temp = $sec_query;
    $args =  get_post_meta(get_the_ID(), 'job_tag', true);
    $args = 'tag='.$args.'&posts_per_page=12&paged='.$paged);
    $sec_query = new WP_Query( $args );
    ?>

    By explicitly using the query() method, you end up making two separate queries, discarding one. It’d be better if you used the array form of args instead of string, but string form still works OK.

    I’ve no idea how ja_pagination() works, but if it’s like most, you cannot paginate custom queries with it. Nearly all pagination functions work off of the default main query, never custom queries. For this reason its usually better to alter the main query through “pre_get_posts” action instead.

    If you must paginate a custom query, do so with paginate_links(). Check out the user notes near the bottom of the linked page for examples of typical usage.

    Thread Starter Xaibi Aslam

    (@lahorimela)

    This is the pagination code @bcworkz..

    <?php
    /* WordPress Pagination
     * author: Dimox
     * version: 2.1
    */
    function ja_pagination() {
    	global $wp_query, $wp_rewrite;
    	$pages = '';
    	$max = $wp_query->max_num_pages;
    	if (!$current = get_query_var('paged')) $current = 1;
    	$a['base'] = str_replace(999999999, '%#%', get_pagenum_link(999999999));
    	$a['total'] = $max;
    	$a['current'] = $current;
    
    	$total = 1; //1 - display the text "Page N of N", 0 - not display
    	$a['mid_size'] = 2; //how many links to show on the left and right of the current
    	$a['end_size'] = 1; //how many links to show in the beginning and end
    	$a['prev_text'] = '<i class="ti-arrow-left"></i> Previous'; //text of the "Previous page" link
    	$a['next_text'] = 'Next <i class="ti-arrow-right"></i>'; //text of the "Next page" link
    
    	if ($max > 1) echo '<div class="pagination">';
    	//if ($total == 1 && $max > 1) $pages = '<ul class="pagination">Page ' . $current . ' of ' . $max . '</ul>'."\r\n";
    	echo $pages . paginate_links($a);
    	if ($max > 1) echo '</div>';
    }
    • This reply was modified 3 months, 1 week ago by Xaibi Aslam.
    Moderator bcworkz

    (@bcworkz)

    Heh, that’s using paginate_links() to paginate the main query šŸ™‚ It has no idea what your custom query is doing. Is this your function or from a theme/plugin by others? If not yours, make your own version of it before altering it.

    Its main issue is it’s using the main query to determine max pages. Change the $max assignment to $max = $sec_query->max_num_pages;

    Thread Starter Xaibi Aslam

    (@lahorimela)

    Thankyou @bcworkz, actually I am not a dev, I don’t know the code. I do search and make implement on-site. I buy an HTML template and add WordPress code inside. So I shall be very thank you to you if you provide the full code. šŸ™

    I change the max code but not working.

    Even this code is not working as well.

    <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
    <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>

    I think my first code is not fine.

    • This reply was modified 3 months, 1 week ago by Xaibi Aslam.
    • This reply was modified 3 months, 1 week ago by Xaibi Aslam.
    • This reply was modified 3 months, 1 week ago by Xaibi Aslam.
    Thread Starter Xaibi Aslam

    (@lahorimela)

    How I can convert my code into New code.

    My Code:

    <?php 
    $temp = $sec_query;
    $sec_query= null;
    $sec_query = new WP_Query();
    $args =  get_post_meta($post->ID, 'job_tag', true);
    $sec_query->query('tag='.$args.'&posts_per_page=12&paged='.$paged);
    ?>

    New Code:

    <?php
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $query = new WP_Query( array(
            'category_name' => 'investor-news',
            'posts_per_page' => 2,
            'paged' => $paged
        ) );
    ?>
    Moderator bcworkz

    (@bcworkz)

    If you’re able to cobble together working code from examples, you’re not totally lost with coding. What you’re doing is the first step to becoming better able to custom code. The next step is being able to make bolder, more extensive changes to existing code. You need to be able to experiment without fear of crashing the site. The best way to do that is to have a local WP installation you can experiment on.

    Anyway, converting your query to array form will not help with pagination. The posts link functions suffer the same problem, relying upon the default main query instead of yours. If your page only needs this one query to show what you want, it’s better to alter the main query through “pre_get_posts” action instead of making a new one. If that would work for you, I can help you translate your new query into a pre_get_posts modification.

    If you go this route, the ja_pagination or the posts link functions will work as expected without modification.

    Thread Starter Xaibi Aslam

    (@lahorimela)

    @bcworkz Thank you again, but I told you I copy codes and test them but can’t change the codes or edit them. As I don’t know the coding. šŸ™

    Thread Starter Xaibi Aslam

    (@lahorimela)

    @bcworkz please help me, brother, looking for your kind reply. thanks

    Moderator bcworkz

    (@bcworkz)

    I’m afraid it’s not so simple. When you link to page 2 or greater, WP will try to query for page 2+ of $post->ID, which doesn’t exist and the main query will fail before we ever get to your custom query. The fact it worked before without resetting post data is a fluke due to the wrong global query data being set. The paginate links scheme only works for archive-like main queries, not single page main queries.

    Maybe instead of linking to page 2+ of the current $post->ID, link to a proper tag archive? Something like
    <a href="<?php echo site_url('/tag/' . get_post_meta(get_the_ID(), 'job_tag', true) . '/page/2/'); ?>">More</a>

    Thread Starter Xaibi Aslam

    (@lahorimela)

    Ok, let me explain my code…

    <?php $count = 1; ?>
    
    <?php 
    $temp = $sec_query;
    $sec_query= null;
    $sec_query = new WP_Query();
    $args =  get_post_meta($post->ID, 'job_tag', true);
    $sec_query->query('tag='.$args.'&posts_per_page=12&paged='.$paged);
    ?>
    
     
    <?php if ( $sec_query->have_posts() ) : ?>
     
    
    <?php while ( $sec_query->have_posts() ) : $sec_query->the_post(); ?>
     
    <?php get_template_part('includes/job-loop'); ?>
    
    		<!--Injecting Code-->
    			<?php if ($count == 6) : ?>
    				<div style="padding: 10px;">
    				<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    					<!-- JA Footer Responsive -->
    					<ins class="adsbygoogle"
         					style="display:block"
         					data-ad-client="ca-pub-5329569690082271"
         					data-ad-slot="4525903855"
         					data-ad-format="auto"></ins>
    				<script>
    				(adsbygoogle = window.adsbygoogle || []).push({});
    				</script>
    				</div>
    			<?php endif; $count++; ?>
    		<!--Injecting Code-->
    
    <?php endwhile; ?>
     
    <?php wp_reset_postdata(); ?>
     
    <?php else: ?>
     
    <?php _e( 'Sorry, no posts matched your criteria.' ); ?>
     
    <?php endif; ?>
    
    							</div>
    					<!--Pagination-->
    						<?php if (function_exists('ja_pagination')) ja_pagination(); ?>

    This is for injecting ads in the loop.

    <?php $count = 1; ?>

    This is the code in which i am using ACF plugin and i add a ACF Field

    <?php 
    $temp = $sec_query;
    $sec_query= null;
    $sec_query = new WP_Query();
    $args =  get_post_meta($post->ID, 'job_tag', true); /** THIS LINE, I ADD ACF FIELD***/
    $sec_query->query('tag='.$args.'&posts_per_page=12&paged='.$paged);
    ?>

    Then other codes you know it. In the last, I use pagination code. and the Pagination code is above, I have mentioned.

    Moderator bcworkz

    (@bcworkz)

    Basically, custom queries and WP pagination functions don’t work together. Using the “paged” query var in paginate_links() is a kludge that doesn’t always work as expected. Ideally, we should develop out own pagination scheme to go with the custom query. Or better yet, modify the main query through “pre_get_posts” so you don’t need a custom query. Then WP pagination functions work as expected.

    Thread Starter Xaibi Aslam

    (@lahorimela)

    So any better solution, because I cant code.

    Moderator bcworkz

    (@bcworkz)

    You essentially need a custom query var to use in place of $paged. If you are unable to piece together something from examples, you may need to hire someone to help you. A couple possible resources are https://jobs.wordpress.net/ or https://jetpack.pro/

Viewing 13 replies - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.