• Resolved rayo2010

    (@rayo2010)


    I have 3 loops in the same page

    <?php while ( have_posts() ) : the_post(); ?>
    
    					<?php
    
    						$exp_date = get_post_meta($post->ID, "mp_end_date", true);
    
    						if( $exp_date >= date("Ymd") ){
    
    						$package_type= get_post_meta($post->ID, "package_type", true);
    						$type1="Gold";
    						$type2="Platinum";
    						$type3="Normal";
    
    						while ($package_type == $type1) :?>
    					<li>
    						<?php get_template_part( 'snippit', $post->post_type ); $package_type=""; ?>
    					</li>
    					<?php endwhile;?> 
    
    					<?php	} ?>
    
    				<?php endwhile; ?>

    and

    <?php	while ( have_posts() ) : the_post(); ?>
    
    					<?php
    
    						$exp_date = get_post_meta($post->ID, "mp_end_date", true);
    
    						if( $exp_date >= date("Ymd") ){
    
    						$package_type= get_post_meta($post->ID, "package_type", true);
    						$type1="Gold";
    						$type2="Platinum";
    						$type3="Normal";
    
    						while ($package_type == $type2) :?>
    					<li>
    						<?php get_template_part( 'snippit', $post->post_type ); $package_type=""; ?>
    					</li>
    					<?php endwhile;?> 
    
    					<?php	} ?>
    
    				<?php endwhile; ?>

    and

    <?php	while ( have_posts() ) : the_post(); ?>
    
    					<?php
    
    						$exp_date = get_post_meta($post->ID, "mp_end_date", true);
    
    						if( $exp_date >= date("Ymd") ){
    
    						$package_type= get_post_meta($post->ID, "package_type", true);
    						$type1="Gold";
    						$type2="Platinum";
    						$type3="Normal";
    
    						while ($package_type == $type3) :?>
    					<li>
    						<?php get_template_part( 'snippit', $post->post_type ); $package_type=""; ?>
    					</li>
    					<?php endwhile;?> 
    
    					<?php	} ?>
    
    				<?php endwhile; ?>

    all those loops are in the same Cat . and then i use
    <?php kriesi_pagination($wp_query->max_num_pages);?>
    The problem here there are always some posts from the first loop on the 2nd page , and i want the first loop posts then the 2nd then the 3rd. i dont know whats wrong here.

    [bump moderated]

Viewing 14 replies - 1 through 14 (of 14 total)
  • Try This code for page navigation

    1.Paste this code in function.php file

    function pagination($pages = '', $range = 4)
    {
         $showitems = ($range * 2)+1;  
    
         global $paged;
         if(empty($paged)) $paged = 1;
    
         if($pages == '')
         {
             global $wp_query;
             $pages = $wp_query->max_num_pages;
             if(!$pages)
             {
                 $pages = 1;
             }
         }   
    
         if(1 != $pages)
         {
             echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
             if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
             if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
    
             for ($i=1; $i <= $pages; $i++)
             {
                 if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                 {
                     echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                 }
             }
    
             if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
             if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
             echo "</div>\n";
         }
    }

    2.Then paste this code in loop.php file or index.php file.

    <?php if (function_exists("pagination")) {
        pagination($additional_loop->max_num_pages);
    } ?>

    3.Now style your Page Navigation. Add this code in style.css file

    .pagination {
    clear:both;
    padding:20px 0;
    position:relative;
    font-size:11px;
    line-height:13px;
    }
    
    .pagination span, .pagination a {
    display:block;
    float:left;
    margin: 2px 2px 2px 0;
    padding:6px 9px 5px 9px;
    text-decoration:none;
    width:auto;
    color:#fff;
    background: #555;
    }
    
    .pagination a:hover{
    color:#fff;
    background: #3279BB;
    }
    
    .pagination .current{
    padding:6px 9px 5px 9px;
    background: #3279BB;
    color:#fff;
    }

    Thread Starter rayo2010

    (@rayo2010)

    it didnt work :/ . it does the same problem.
    how many posts will that code show per page?

    ps: Thank you so much for replying.

    EDIT:

    Using the above code after exactly 10 posts it goes all wrong.
    i mean if 1st loop has 4 posts and 2nd has 3 posts and the 3rd has 3 posts
    it will work fine but as soon as i add a new post for the 2nd loop a post from the 1st loop goes to the 2nd page -_- ..!!!

    Moderator keesiemeijer

    (@keesiemeijer)

    Can you paste and submit the full code (with the three loops) and the snippit template into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    First try to get pagination working with native WordPress pagination functions.

    Thread Starter rayo2010

    (@rayo2010)

    Well i think the pagination aint the problem. i removed the paginator but still the page dont post more than 9 posts.
    i have a category A which contain 11 posts but only 9 appears.
    and its not a problem with the posts cuz i shuffled them but still only 9 appears.
    any sight of the reason?!!
    ——————————————-
    here is the full code of 3 loops:
    https://pastebin.com/C0cY4eKQ

    and the snippit temp:
    https://pastebin.com/yU9egGfB

    Thx

    Thread Starter rayo2010

    (@rayo2010)

    Edit:

    i went to setting > reading settings and changed the option Blog pages show at most from 10 to 60 . and it showed all the posts but now the pagination function doesn’t work at all ??

    Moderator keesiemeijer

    (@keesiemeijer)

    The problem is you can only paginate one (the main) loop. What is it exactly you want to do. Have the first loop on the first page, the second on the paginated second page, etc. ?

    Also, try to remove this:

    $max_num_pages=2;

    Thread Starter rayo2010

    (@rayo2010)

    i have 3 types of posts , GOLD , platinum and Normal.
    i want the Gold posts to be shown first then the platinum and then the normal and every page only have 9 posts.

    so if Gold –> 6 posts and Plat —> 5 posts and Normal —> 3 posts

    then first page will have 6 Gold posts and only 3 Plat posts and the next page has the rest of plat posts (2) and the normal posts (3)

    Thread Starter rayo2010

    (@rayo2010)

    Moderator keesiemeijer

    (@keesiemeijer)

    Is this in a custom taxonomy template?

    Thread Starter rayo2010

    (@rayo2010)

    yes it is.

    Moderator keesiemeijer

    (@keesiemeijer)

    We have to try and merge all three loops so pagination will work. Try putting this in your theme’s functions.php:

    add_action( 'pre_get_posts', 'my_post_queries' );
    function my_post_queries( $query ) {
    	// not an admin page and is the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    		// only on resource_categories pages
    		if ( is_tax( 'resource_categories' ) ) {
    
    		  // custom field query
    			$meta_query = array(
    				array(
    					'key' => 'package_type',
    					'value' => array( 'gold', 'platinum', 'normal' ),
    				)
    			);
    			$query->set( 'meta_query', $meta_query );
    			add_filter( 'posts_orderby', 'order_by_meta_key' );
    		}
    	}
    }
    
    function order_by_meta_key( $orderby ) {
    	global $wpdb;
    
    	$orderby = "
      $wpdb->postmeta.meta_value = 'gold' DESC,
      $wpdb->postmeta.meta_value = 'platinum' DESC,
      $wpdb->postmeta.meta_value = 'normal' DESC,
      $wpdb->posts.post_date DESC";
    
    	return $orderby;
    }

    Maybe you have to change if ( is_tax( 'resource_categories' ) ) { to the correct taxonomy name.
    This will order your posts the way you want on the ‘resource_categories’ pages.
    Just use one loop in the archive template:
    https://pastebin.com/kHCGJPFb

    Thread Starter rayo2010

    (@rayo2010)

    Hay Thank You SOOO MUCH , it worked like a charm ?? .

    i know u may be busy and u already spent a lot of time working on my issue but can you please ‘if u r free’ explain that code for me?.

    Here You made a meta array with the meta values i want to use

    $meta_query = array(
    				array(
    					'key' => 'package_type',
    					'value' => array( 'gold', 'platinum', 'normal' ),
    				)
    			);

    I dont understand what this line will do

    $query->set( 'meta_query', $meta_query );
    add_filter( 'posts_orderby', 'query_term_ratings', 10, 3 );

    And then you ordered the posts in the database but i dont fully understand the logic here

    function query_term_ratings( $orderby ) {
    	global $wpdb;
    
    	$orderby = "
      $wpdb->postmeta.meta_value = 'gold' DESC,
      $wpdb->postmeta.meta_value = 'platinum' DESC,
      $wpdb->postmeta.meta_value = 'normal' DESC,
      $wpdb->posts.post_date DESC";
    
    	return $orderby;
    }

    so i hope when u have time to explain ur code but again THANK YOU SO MUCH.

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome.
    You can query (alter the loop) with the “pre_get_posts” action from your functions.php file. This is the preferred way on pages where you show a list of posts (categories, home page, search, etc). With $query->set( ‘key’, ‘value’ )` method you can set a new value for the query parameters. So it sets the meta_query value for the meta_query parameter.
    See https://www.billerickson.net/customize-the-wordpress-query/

    Because WordPress doesn’t have ordering posts by meta key you have to change the “ORDER BY” clause in the sql that WordPress uses to retrieve the posts from the datatabse. This is done with the ‘posts_orderby’ filter.

    The logic is that it orders first by the ‘gold’ meta_key, second by the ‘platinum’ meta_key, third by the ‘normal’ meta_key and fourth by post date.

    I’m glad you’ve got it resolved

    Thread Starter rayo2010

    (@rayo2010)

    Thank You Very Much ??

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘pagination with 3 loops’ is closed to new replies.