WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WordPress 3.4 broke my paginaton setup (24 posts)

  1. jmwhite19
    Member
    Posted 2 years ago #

    I updated WordPress to 3.4 on my localhost environment this morning and found it broke my pagination setup I have for a certain theme I developed.

    Basically for the index.php I have the specific pagination setup of one article per page. For archives and everything else the standard WordPress settings are used. This was working in 3.3.2 but since upgrading to 3.4 the pagination setup for the index.php was broken.

    Here is the code I'm using. I'm aware that it maybe a hackish type solution so Im open to suggestion on how to improve it/get it working in 3.4.

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('posts_per_page=1&paged=' . $paged);
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>			
    
    <!--The post -->
    
    <?php endwhile; ?>
    
    <?php else : ?>
    
    <h2>Not Found</h2>
    
    <?php endif; ?>
    ?>
  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it without the query_posts() and with this in your theme's functions.php:

    function my_post_queries( $query ) {
      // not an admin page and it is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_home()){
          $query->set('posts_per_page', 1);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

  3. jmwhite19
    Member
    Posted 2 years ago #

    Thanks! That worked great, I downgraded back to 3.3.2, so I need to upgrade again and confirm. Looks promising though!

    Edit: Works well in 3.4. Thanks again!

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    You're welcome. Let me know if it still works in 3.4.

  5. UsagiYojimbo
    Member
    Posted 2 years ago #

    You make my day. I experienced equivalent issues and your trick solve it !

  6. cinematic
    Member
    Posted 2 years ago #

    thanks keesiemeijer, works like a charm!

  7. adnanoner
    Member
    Posted 2 years ago #

    Hi guys,

    I'm using a plugin (wp-ecommerce) and it's pagination also broke with the new WP 3.4 upgrade. I downgraded now but would like to upgrade. The code where the WP-query is:

    function wpsc_a_page_url($page=null) {
    	global $wp_query;
    	$output = '';
    	$curpage = $wp_query->query_vars['paged'];
    	if($page != '')
    		$wp_query->query_vars['paged'] = $page;
    	if($wp_query->is_single === true) {
    		$wp_query->query_vars['paged'] = $curpage;
    		return wpsc_product_url($wp_query->post->ID);
    	} else {
    		if( 1 < $wp_query->query_vars['paged']) {
    			if(get_option('permalink_structure'))
    				$output .= "paged/{$wp_query->query_vars['paged']}/";
    			else
    				$output = add_query_arg('paged', '', $output);
    
    		}
    	return $output;
    	}
    }

    There is also a function for the pagination creation but I don't think I need to modify that. I have pasted it in pastebin though: http://pastebin.com/Ty2GtdY0

    Any ideas on how to modify it so that it works?

  8. solutionsphp
    Member
    Posted 2 years ago #

    Same issue with a Thematic theme. Waiting for the developers to issue a fix.

  9. jmwhite19
    Member
    Posted 2 years ago #

    Any chance this can modified to work with sticky posts?

    I was thinking if it would be possible to use this code to display one post per page (as is now) but if a sticky post is set, display that post on the frontpage first and then pagination every other post?

    What would be the best approach to this? Modify the function or write some conditionals in the index.php?

  10. keesiemeijer
    moderator
    Posted 2 years ago #

    @jmwhite19
    Do you mean the my_post_queries function in my first post in this topic?

    It is very tricky to have one loop and stickies to adhere to the (posts_per_page) pagination rules. I made it so that if you have sticky posts their post IDs will be merged with all other post IDs.

    After they are merged the query on the home page will use the 'post_in' argument to display the correct (paginated) posts.
    http://codex.wordpress.org/Function_Reference/WP_Query#Post_.26_Page_Parameters

    The merged (IDs) results are created and put in the database once (when a visitor visits your home page) so WordPress doesn't have to merge all ID's on all paginated pages. The merged (IDs) results will be deleted if you publish or edit a post. Just visit your home page once and all the correct (merged) post id's are back in the database

    I wouldn't use this if your website has many posts as the array with ID's in the database will grow very big.

    I hope that made sense.

    Here are the new functions for in your functions.php: http://pastebin.com/zDJ5nsEr

    (delete the old my_post_queries functions)

  11. jmwhite19
    Member
    Posted 2 years ago #

    Wow, your awesome! Your function works great. Just tested it out. Sticky post stays on top and all other posts paginate to one each page correctly.

    I cannot thank you enough! Hopefully your code can help others as well as me. I know the previous function for just limiting to one post has helped several others already!

    Thanks again!

  12. keesiemeijer
    moderator
    Posted 2 years ago #

    You're welcome. But if you have a site with more than 5000 posts I wouldn't use this second solution because it doesn't scale well. But then again I don't know a better solution to have sticky posts adhere to paginagion.

  13. evo_x
    Member
    Posted 2 years ago #

    I have the same problem.

    After I have added this code to theme functions

    function my_post_queries( $query ) {
      // not an admin page and it is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_home()){
          $query->set('posts_per_page', 1);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    the pagination of the home/blog posts works correctly, but search, category and tags pagination is still does not work after a certain number of pages, what can I do ?

  14. IfeanyiG
    Member
    Posted 2 years ago #

    Hi, please i use wordpress 3.4, and i was playing around in the header.php

    the domain name is http://www.waohnaija.com, i was playing around with some new backgrounds, and it was working very well, until i decided to remove an ad code i had put before, i dont know what i did to spoil everything

  15. keesiemeijer
    moderator
    Posted 2 years ago #

    @evo_x @IfeanyiG @adnanoner
    Please create your own topics.

  16. cmfcom
    Member
    Posted 2 years ago #

    Thank you, keesiemeijer! This fixed my page issues as well!

  17. chand_4rana
    Member
    Posted 2 years ago #

    Hello,

    Here is my product listing code
    <?php
    global $posts,$Product;
    $temp = $wp_query; $wp_query= null;
    $wp_query = new WP_Query('posts_per_page='.$limit.'&post_type=products&paged=' . $paged);
    if($wp_query->have_posts())
    {
    ?>
    <?php _e('Switch View');?>
    <ul style="display: block;" class="display <?php echo $General->all_product_listing_format();?> category_list">
    <?php
    while($wp_query->have_posts())
    {
    $wp_query->the_post();
    $Product->product_listing_li($post);
    }
    ?>

    <?php } ?>

    with this code pagination not work
    what is wrong in this can anybody solve this issue

    Thanks

  18. Alex
    Member
    Posted 2 years ago #

    The solution above didnt work for me. Page 2 works fine, not 3 4 5 etc

  19. Alex
    Member
    Posted 2 years ago #

    fixed by adding this to functions.php

    just change "CUSTOM POST TYPE HERE" with your post type. example: post, events, or gallery

    function my_query_for_homepage( $query ) {
    if( $query->is_main_query() && $query->is_home() ) {
    $query->set( 'post_type', array( 'CUSTOM POST TYPE HERE' ) );
    }
    }
    add_action( 'pre_get_posts', 'my_query_for_homepage' );

  20. evo_x
    Member
    Posted 2 years ago #

    this code solved the problem for me only for the homepage index, in categories and tags still the same problem, did you check the tags and categories ?

  21. Alex
    Member
    Posted 2 years ago #

    then just add
    $query->set( 'post_type', array( 'CUSTOM POST TYPE HERE' , 'tag', 'category' ) );

  22. aljuk
    Member
    Posted 2 years ago #

    @ Alex - brilliant. Thanks for that.

  23. andreleibovici
    Member
    Posted 2 years ago #

    After upgrading to WP 3.4 a template I use to display all posts in an archive list format stopped working. Any tips?

    <?php get_header(); ?>
    <?php
    /*
    Template Name: All posts
    */
    ?>
    <?php
    $debut = 0; //The first article to be displayed
    ?>
    <?php while(have_posts()) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <ul>
    <?php
    $myposts = get_posts('numberposts=-1&offset=$debut');
    foreach($myposts as $post) :
    ?>
    <li><?php the_time('d/m/y') ?>: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    
    <?php get_footer(); ?>
    <?php endwhile; ?>
  24. keesiemeijer
    moderator
    Posted 2 years ago #

    @andreleibovici
    Please create your own topic.

    http://wordpress.org/support/forum/how-to-and-troubleshooting#postform

Topic Closed

This topic has been closed to new replies.

About this Topic