WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Prioritizing posts by category in a date-sorted loop (40 posts)

  1. ggg377
    Member
    Posted 1 year ago #

    Hi. To start it off, my loop is sorted by dates. The posts are grouped and the dates appear once on top of posts of the same date like this:

    --Date--
    Post1
    Post2
    Post3

    --Date before that--
    Post 4
    Post 5

    etc

    I'm trying to achieve a result, where if a post belongs to a certain predetermined category, it would be moved above others under a date as such:

    --Date--
    Post3 (special category)
    Post1
    Post2

    --Date before that--
    etc

    I can't think of anything. Is achieving such a result even possible with php and WordPress codex? I would be glad if anyone could point me towards helpful materials or some code, because I lack the general idea of how to execute this concept.

    My loop: http://pastebin.com/Pp0rA5j7
    Sample site: http://goldenred.web44.net (I would like to move the post test 6 with the category "test" above others under February 3, 2013)

    I would be really thankful if anyone could help.

  2. ggg377
    Member
    Posted 1 year ago #

    Did some additional research on the topic and found something that seemed somewhat relevant to the topic. I'm not sure though.

    http://codex.wordpress.org/Function_Reference/query_posts
    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    Would it be possible to add "parent" status to the date labels and use conditional tags on everything before the next parent or something similar?

    I'm thinking a request like this would definitely have something to do with the query parameters, multiple loops or functions. Maybe creating the posts with one loop and sorting in another or something of that nature. I'll keep on looking for the answer and I would really appreciate if someone took the time to comment on this endeavor.

    It's quite difficult in the sense that there's not much information on a such a layout, but I'll try and manage. If anyone knows of a GPL theme that uses categorizing like that I would appreciate if you could mention it.

    I would be extremely thankful for anything that could help me. I'm prepared that this could take me a very long while.

  3. ggg377
    Member
    Posted 1 year ago #

    Alright I'm starting to think this could be done with queries. http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    I found a plugin (http://wordpress.org/extend/plugins/my-posts-order/) that claims to do almost what I need, but I'm not really sure how to implement it with my date sorting. I think it also uses the query functions and I generally prefer to have the least amount of plugins possible, but I'll go with it if necessary and it's possible to implement it. At least I'm starting to get some idea of what needs to be done.

    Any comments would be greatly appreciated.

  4. ggg377
    Member
    Posted 1 year ago #

    Alright, since I posted this thread I've managed to obtain this code, which is unfortunately broken (posts don't show):

    <?php
    $w_h = $w_d = $last = 0;
    // init variables to concatenate content later
    $primary_posts = $secondary_posts = '';
    // empty array to fill later
    $category_names = array();
    if (have_posts()) :
        while (have_posts()) :
            the_post();
            if ( date('Yz') == get_the_time('Yz') ) {
                if (!$w_d++) echo '<h6>Today</h6>';
            } elseif ( date('Yz')-1 == get_the_time('Yz') ) {
                if (!$w_h++) echo '<h6>Yesterday</h6>';
            } else {
                echo the_date('', '<h3>', '</h3>');
            };
            // get post categories
            $category_objects = get_the_category();
            foreach($category_objects as $category_object) {
                $category_names[] = $category_object->name;
            }
            // if posts belongs to category 'test'
            if( in_array('test', $category_names) ) {
                $primary_posts .= '<div class="post">Post of category "test"';
                // title, categories and excerpt goes here
                $primary_posts .= '</div><!-- .post -->';
            }
            else {
                $secondary_posts .= '<div class="post">Post of category other than "test"';
                 // title, categories and excerpt goes here
                $secondary_posts .= '</div><!-- .post -->';
           }
    
        endwhile;
        // output all posts of category "test"
        echo $primary_posts;
        // output all posts of category other than "test"
        echo $secondary_posts;
    endif; ?>

    The author of the code thought it was a bad idea in the end, but maybe that's enough to help someone think towards the right direction. The general idea was to collect 2 variables, posts under the category test and all others, and print the posts marked with test before the others within the group. I can't comment much on it otherwise, other than it may be an important step towards what's needed.

    I've also received suggestions to use custom meta fields for high priority posts ('test' directory) and WP_Query to achieve the desired result.

    This is merely a quest I'm after for myself. This isn't a job for a client or anything. I would be extremely glad if anyone could help, because this feature would be a core function to my website.

    If you don't have any ideas about the code, could you please comment on the possibility of this idea, because if I fail to receive help from here, I might hire a freelancer to do the job. I wouldn't want to get ripped off if this turns out to be an insanely difficult request. What makes me cautious is that this one of the very few times where I haven't actually received any help or tips after quite a bit of research.

    I'm also willing to switch the base mechanism of how the posts are sorted into groups towards a possibly more flexible option. I found something like this (using WP_Query): http://pastebin.com/McM8iwB3. Maybe then it would be easier to implement the prioritizing system within the group? Out of the box solutions have often proved to be the key to my hardest problems.

    I'm looking forward to your responses.

    Best regards,
    ggg377

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    Do you want pagination for this page?

  6. ggg377
    Member
    Posted 1 year ago #

    Thanks for your reply, keesiemeijer!

    As for pagination, it doesn't really matter as long as the needed functionality is there. Hopefully I'll be able to implement infinite scrolling on it sometime in the future, but that's a different topic.

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    Without pagination, you don't have the problem with a post from the same date (and maybe in the special category) on the next page. With pagination it makes it very complicated. You could do a (very expensive) query for all posts and reorder them how you want with the "post__in" parameter. But if you have a lot of posts (lets say over 1000) this will become a very large query (on the database) on pages where you need this functionality. I've never used infinite scrolling so I don't know if it uses the pagination from WordPress (logically I think it does).
    If someone knows, please chime in.

    I'm sorry but I don't really have time to look into this right now.

  8. ggg377
    Member
    Posted 1 year ago #

    I can assure you the blog will be dealing with a big number of posts (that's why I'm using grouping like that in the first place). Would going with no pagination mean hundreds or even thousands of posts being displayed at once on the home page? I think if that was the case, I'd be better off without the needed functionality and pagination.

    Another thing is, I personally don't see a problem if a post from the same date group appears on the next page, as that happens now as well, however if the issue is with the script "not reaching" the posts that appear on next pages then that's definitely understandable. Which I guess would leave me with 2 pretty bad choices - no pagination, or a cumbersome query. However if that's only a navigation/cosmetic issue then that wouldn't be a problem. Oh well. I'm open for more ideas.

    By the way infinite scrolling does use pagination, you usually place the selectors around the pagination functions in your theme.

    Anyway, thanks a lot for contributing. Hopefully one day :)

  9. keesiemeijer
    moderator
    Posted 1 year ago #

    Here is an example to give you an Idea how you could do this with multiple loops. The first loop gets all the id's of the current page and sorts them by day with the specific category moved to the top. The second loop re-uses these id's for the display of the posts in the right order. This lets you also have pagination if you want:

    <?php 
    
    	$date_ids = array();
    
    	// normal loop to get all post IDs by date and category
    	while ( have_posts() ) { the_post(); 
    
    		$post_date = date('Ynj', strtotime( $post->post_date ));
    
    		// change the category ID
    		if(in_category(1)) {
    			$date_ids[$post_date]['first'][] = $post->ID;
    		} else {
    			$date_ids[$post_date]['last'][] = $post->ID;
    		}
    
    	} // end of normal loop
    
    	// sort all IDs
    	$all_ids = array();
    
    	foreach($date_ids as $key => $date){
    
    		if(isset($date['first'])) {
    			$all_ids = array_merge($all_ids, $date['first'] );
    		}
    		if(isset($date['last'])){
    			$all_ids = array_merge($all_ids, $date['last'] );
    		}
    	}	
    
    ?>
    
    <!-- put pagination functions before the display loop here -->
    
    <?php
    
    	// use a custom loop to show the posts in the correct order
    	// this way we don't mess with the pagination
    
    	$args =  array(
    	  'post__in' => $all_ids,
    	  'orderby' => 'post__in',
    	  'ignore_sticky_posts' => 1
    	);
    
    	$the_query = new WP_Query( $args ); ?>
    
    <!-- the loop to display the posts -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    	<!-- put your loop code here -->
    	<?php the_title(); ?>
    
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    
    <!-- put pagination functions after the display loop here -->

    Change the category ID in if(in_category(1)) to the category you want on top of the days posts.

  10. ggg377
    Member
    Posted 1 year ago #

    I couldn't thank you enough keesiemeijer. Your concept as well as the code work as needed.

    However, I have encountered two minor bugs as well. The dates "Today" and "Yesterday" receive the date label for each post for some reason, even though the code is still the same. In the case of "the_date", it works exactly as intended. This is the relevant code:

    <?php $w_h = $w_d = $last = 0; ?>
    
                    <div class="indexdate"><?php
        if ( date('Yz') == get_the_time('Yz') ) {
            if (!$w_d++) echo '<h6>Today</h6>';
        } elseif ( date('Yz')-1 == get_the_time('Yz') ) {
            if (!$w_h++) echo '<h6>Yesterday</h6>';
        } else {
            echo the_date('', '<h3>', '</h3>');
        };
    ?>

    Also this code gets implemented for every post now, even though it's intended only for the first posts of groups.

    <div id="post" <?php if( get_the_time('Yz') != $last ) { $last = get_the_time('Yz'); echo ' class="first" '; } ?>>

    I think it conflicts with an earlier code, but I'm not sure.

    index.html: http://pastebin.com/7aeA8qrU
    Sample of the code in action and the bug: http://goldenred.web44.net

  11. ggg377
    Member
    Posted 1 year ago #

    Ugh, I noticed another problem. Sometimes, posts marked with "test" appear on the second page even when the date label is on the first page and there are no other posts marked as "test" under that date label. The post marked with "test" is moved to the top of the second page instead of being moved on the first page to the top of the list.

    Actually that's another issue that I need to take care of soon. I don't want the date labels to appear twice under any circumstances (even if posts for a particular date appear through many pages). I suppose if it would be possible to remove duplicate displays of date labels on multiple pages, that problem would be automatically taken care of (and that would also save me from looking into that in the future).

    Of course if it's possible to take care of that in another manner then I would be happy to try that, because achieving the objectives of this thread is the highest priority. The previous paragraph was just something that I thought would be useful to mention.

    I'm looking forward to all replies and I hope I'm not causing too much trouble. Once again, thank you for your effort keesiemeijer, you've already done enough truthfully.

  12. keesiemeijer
    moderator
    Posted 1 year ago #

    What do you want to do? Have one header for the "today" and "yesterday" posts, or have the header on all "today" and "yesterday" posts? Should all "today" and "yesterday" posts get a "first" class?

  13. ggg377
    Member
    Posted 1 year ago #

    "Today" and "yesterday" posts should only receive one header per group (per one date). So if there's 3 posts from 02/03/2013 for example, these should only receive one header.

    Only the topmost posts from the "today", "yesterday" and "the_date" groups should receive "first" class so that it would be possible to remove the top border.

  14. keesiemeijer
    moderator
    Posted 1 year ago #

    Another thing you could do is loop through dates instead of through posts. This way all date posts are on the same page. Here is an example: http://pastebin.com/KhSvg1uz
    Change the category ID in if(in_category(1)) to the category you want on top of the same date posts.

    To see the pagination work go to /page/2. Unfortunatly I don't have any more time to work on this today.

  15. ggg377
    Member
    Posted 1 year ago #

    That is an impressive idea as well as coding. I am really thankful you're willing to put in effort to help me. The last bit of code you posted solved all pagination issues.

    The only problems left are the date labels being shown on all posts in "Today" and "Yesterday" (seeing how it works for "the_date" it can't be that bad) and getting different styling for top posts within dates to work. I'll get to solving these problems now. I'll report if it proves to be too difficult. Once again, thank you.

  16. ggg377
    Member
    Posted 1 year ago #

    Edit: found another issue. Only a maximum of 5 posts get shown per date (the "test" posts if there are any and the newest posts of other categories), an example is February 3, I have another post on that date, which isn't shown. Other posts are hidden.

    Unfortunately I think I won't be able to figure that out. Hopefully it's possible to get that resolved, because overall, the code is exactly what I need.

  17. keesiemeijer
    moderator
    Posted 1 year ago #

    Yes, I also found some issues with pagination, that's why I moved most of the functionality to the functions.php file.
    Try it with this in your theme's functions.php file: http://pastebin.com/BuH4CH3c
    You can change the $dates_per_page and $top_category_id in:

    // change these two values
    $dates_per_page = 4; // four dates to loop through (dates can have multiple posts)
    $top_category_id = 1; // category you want on top of same date posts

    Just as a warning, this will only work on your home page.

    In your index.php you can now use a normal loop to display the posts:

    <?php while ( have_posts() ) : the_post(); ?>
      <!-- put your loop code here -->
      <?php the_title(); ?>
    <?php endwhile; ?>

    For getting the right pagination links you can use the functions
    previous_date_link() and next_date_link() that are now in your theme's functions.php. You'll need to pass the $dates_per_page and (optional) a label to these functions. Example:

    <?php
    $dates_per_page = 4; // four dates to loop through (dates can have multiple posts)
    // 'Previous Page' and 'Next Page' are labels
    echo previous_date_link( $dates_per_page, 'Previous Page' );
    echo next_date_link( $dates_per_page, 'Next Page' );
    ?>

    I hope this will work as you want it to.

  18. ggg377
    Member
    Posted 1 year ago #

    keesiemeijer, this is, simply put - awesome. The code indeed does the job exactly as intended. I double-checked and nothing seems to be hidden anymore. The main functionality works.

    However, one more issue (that I mentioned earlier) still remains, it broke a bit of code from my old loop. First, "Today" and "Yesterday" get duplicate date labels for each post. "the_date" works as intended for some reason, posts under that get grouped correctly. Code:

    <?php $w_h = $w_d = $last = 0; ?>
    
                    <div class="indexdate"><?php
        if ( date('Yz') == get_the_time('Yz') ) {
            if (!$w_d++) echo '<h6>Today</h6>';
        } elseif ( date('Yz')-1 == get_the_time('Yz') ) {
            if (!$w_h++) echo '<h6>Yesterday</h6>';
        } else {
            echo the_date('', '<h3>', '</h3>');
        };
    ?>

    Second, the class "#post.first" gets applied to all posts instead of being applied to the first posts of respective categories only. Code:

    <div id="post" <?php if( get_the_time('Yz') != $last ) { $last = get_the_time('Yz'); echo ' class="first" '; } ?>>

    I also drew some illustrations so it would be easier to understand:
    Labels: http://i.imgur.com/C0SO5gz.jpg
    Borders: http://i.imgur.com/S4KfxZo.jpg

    I hope you have some comments or ideas on these issues. Both of these work normally without the enhanced loop. I've had both of these problems since your first scripts. Really hope it's possible to get these fixed.

    We've ventured quite far into unmarked WordPress territory. I hope other WordPress users will find this useful in the future.

  19. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with a loop like this:

    <?php
     $w_h = $w_d = $last = 0;
     $date =  date('Yz');
    ?>
    <?php while ( have_posts() ) : the_post(); ?>
    
    <?php
        $post_date = get_the_time('Yz');
        $class = '';
    
        if ($date == $post_date) {
            if (!$w_d++) echo '<div class="indexdate"><h6>Today</h6></div>';
        } elseif (  $date -1 == $post_date ) {
            if (!$w_h++) echo '<div class="indexdate"><h6>Yesterday</h6></div>';
        } else {
            echo '<div class="indexdate"><h3>' . get_the_date() . '</h3></div>';
        };
    
        if( $post_date != $last ) {
          // new post date
          $last = $post_date;
          $class = ' class="first" ';
        }
    ?>
    
    <div id="post"<?php echo $class ?>>
    
      <!-- put your post code here -->
      <?php the_title(); ?>
    
    </div>
    <?php endwhile; ?>
  20. ggg377
    Member
    Posted 1 year ago #

    keesiemeijer, thank you once again. That almost did it, "Today" and "Yesterday" are perfect, but now, posts under "get_the_date" get labels for each post. #post.first class seems to work correctly everywhere. I hope the label issue can be easily solved.

    For quick reference:

    index.html: http://pastebin.com/nYamPBQV
    Example of everything almost working: http://goldenred.web44.net

  21. keesiemeijer
    moderator
    Posted 1 year ago #

    Try changing this:

    echo '<div class="indexdate"><h3>' . get_the_date() . '</h3></div>';

    to this:

    if( $post_date != $last ) {
      echo '<div class="indexdate"><h3>' . get_the_date() . '</h3></div>';
    }
  22. keesiemeijer
    moderator
    Posted 1 year ago #

    Also for pagination try to change this:

    <div id="postnavi_index">
       <div class="right"><?php next_posts_link('Older &raquo;') ?></div>
       <div class="left"><?php previous_posts_link('&laquo; Newer') ?></div>
    </div>

    to this:

    <div id="postnavi_index">
      <?php $dates_per_page = 4; ?>
       <div class="right"><?php echo previous_date_link( $dates_per_page, 'Older &raquo;' ); ?></div>
       <div class="left"><?php echo next_date_link( $dates_per_page, '&laquo; Newer' ); ?></div>
    </div>
  23. ggg377
    Member
    Posted 1 year ago #

    It worked, which also means this thread has reached its conclusion. You definitely helped solve a complex issue and I am most thankful for that. Your skills are impressive. Instead of just showering you with praise though, I'll try to help people out on the forum instead at times and I'll also try to understand the code, which you wrote, better. This was such a huge help, that it deserves more than just a thank you in my opinion. I hope this knowledge will be useful for other WordPress users as well.

    As a last thing, I figured if you have an idea how to wrap the pagination function in nav tags? I actually used twentytwelve's pagination functions before, which looked like this:

    index.php:

    <?php twentytwelve_content_nav( 'nav-below' ); ?>

    functions.php:

    if ( ! function_exists( 'twentytwelve_content_nav' ) ) :
    /**
     * Displays navigation to next/previous pages when applicable.
     *
     * @since Twenty Twelve 1.0
     */
    function twentytwelve_content_nav( $html_id ) {
    	global $wp_query;
    
    	$html_id = esc_attr( $html_id );
    
    	if ( $wp_query->max_num_pages > 1 ) : ?>
    		<nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
    			<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
    			<div class="nav-previous alignleft"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentytwelve' ) ); ?></div>
    			<div class="nav-next alignright"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?></div>
    		</nav><!-- #<?php echo $html_id; ?> .navigation -->
    	<?php endif;
    }
    endif;

    The important thing was that with this implementation, I received selectors like this: navigation (section) selector: #content nav, next (page) selector: #content nav a. I tried wrapping the functions in your code in nav tags, but it didn't work:

    function next_date_link( $dates_per_page = 10, $label = 'Next Page' ){
                    return previous_date_link( $dates_per_page, $label, false );
            }
    
            function previous_date_link( $dates_per_page = 10, $label = 'Previous Page', $prev = true ){
    
                    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
                    if ( $prev ) {
    
                            $count = (($paged -1) * $dates_per_page) + $dates_per_page;
                            $limit = ' LIMIT ' . $count . ', 1';
    
                    } else {
    
                            $count = (($paged -1) * $dates_per_page)-1;
    
                            if ( ($count+1) < $dates_per_page )
                                    return '';
    
                            $limit = ' LIMIT ' . $count . ', 1';
    
                    }

    Anyway you can leave this for me to figure out. I'm not asking you to help me out with this. It is my last question however.

    Best regards :)

    ggg377

  24. keesiemeijer
    moderator
    Posted 1 year ago #

    The pagination code doesn't use the global $wp_query object that WordPress uses for your home and paged home pages. That's why you can't use this:

    if ( $wp_query->max_num_pages > 1 ) :

    What you could do is something like this in the twentytwelve_content_nav function [untested]:

    <?php if ( is_home() ) :
      // pagination on the home page
      $dates_per_page = 4;
      $prev = previous_date_link( $dates_per_page, 'Older »' );
      $next = next_date_link( $dates_per_page, '« Newer' ); ?>
      <?php if($prev || $next) : ?>
        <nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
          <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
          <div class="nav-previous alignleft"><?php echo $prev;  ?></div>
          <div class="nav-next alignright"><?php echo $next; ?></div>
        </nav><!-- #<?php echo $html_id; ?> .navigation -->
      <?php endif; ?>
    
    <?php else :
    // pagination on all other pages
    ?>
    
      <?php if ( $wp_query->max_num_pages > 1 ) : ?>
        <nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
          <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
          <div class="nav-previous alignleft"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentytwelve' ) ); ?></div>
          <div class="nav-next alignright"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?></div>
        </nav><!-- #<?php echo $html_id; ?> .navigation -->
      <?php endif; ?>
    
    <?php endif; ?>

    This was a complex problem which I think we've solved with the least amount of (large) queries to the database.

    I'm glad you've got it resolved :-)

  25. ggg377
    Member
    Posted 1 year ago #

    Placing that code within this function resulted in a nasty syntax error no matter how I did it:

    function twentytwelve_content_nav( $html_id ) {code here}

    In the end for me it's not that important to use twentytwelve's function, but to get working selectors for #content nav and #content nav a. If that could be done easier within your function, while getting the same functionality then that would be great too. However if I placed your code wrong, then repairing that would be just fine.

  26. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this in your theme's functions to show pagination with the same format as in twentytwelve_content_nav():

    function get_index_pagination_nav( $html_id ) {
    
    	$html_id = esc_attr( $html_id );
    
    	if ( is_home() ) :
    
    		// pagination on the home page
    		$dates_per_page = 4;
    		$prev = previous_date_link( $dates_per_page, 'Older »' );
    		$next = next_date_link( $dates_per_page, '« Newer' );
    
    		if( $prev || $next ) : ?>
    
    			<nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
    				<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
    				<div class="nav-previous alignleft"><?php echo $prev;  ?></div>
    				<div class="nav-next alignright"><?php echo $next; ?></div>
    			</nav><!-- #<?php echo $html_id; ?> .navigation -->
    
    		<?php endif; 
    
    	else :
    		// check if the function exists (index.php is a fallback template for other pages)
    		if ( function_exists( 'twentytwelve_content_nav' ) ) {
    		 	twentytwelve_content_nav( $html_id );
    		}
    
    	endif;
    
    } // end of function get_index_pagination_nav

    Replace the twentytwelve_content_nav function in your index.php with this:

    <?php get_index_pagination_nav( 'nav-below' ); ?>
  27. ggg377
    Member
    Posted 1 year ago #

    That did it keesiemeijer. I don't have any more questions. Thank you for your help and co-operation. I'll try to make it up in time. I hope solving this issue was at least somewhat fun and interesting for you.

    ggg377

  28. keesiemeijer
    moderator
    Posted 1 year ago #

    I hope solving this issue was at least somewhat fun and interesting for you.

    It was. As you can see on my profile page, one of my interests is puzzles, and this was one :-)

    Maybe one last advise would be to style the post navigation for people who browse with javascript off (I see you installed infinite scrolling). Maybe switch the pagination functions around?
    http://goldenred.web44.net/page/2/

    And thank you for contributing to these forums :)

  29. ggg377
    Member
    Posted 1 year ago #

    I'm glad it was a fun challenge. Thanks for pointing out the potential improvement for non-javascript users. I'll look into stylizing the links. On another note, I've tested the loop a bit now and so far it seems perfect. Thank you so much!

  30. keesiemeijer
    moderator
    Posted 1 year ago #

    Just as an update, just today I found out about a cool sql feature "group_concat". With this feature I was able to reduce the queries to the database to one main sql query that returns all post ids sorted by (first) category. I've done some testing and this is much faster than what you have now.

    Back up your functions.php (We know that works).
    remove all my code and replace it with this functions.php code: http://pastebin.com/8uvGYXkb

    Change these values as you like:

    // change these two values
    $dates_per_page = 4; // four dates to loop through (dates can have multiple posts)
    $top_category_id = 1; // category you want on top of same date posts

    The only change I made to the pagination functions is that you have to give the "get_index_pagination_nav" function the $dates_per_page variable, otherwise it will default to 10 dates.
    In index.php:

    <?php
    $dates_per_page = 4;
    get_index_pagination_nav( 'nav-below', $dates_per_page);
    ?>

    Test it out :)

Topic Closed

This topic has been closed to new replies.

About this Topic