WordPress.org

Ready to get started?Download WordPress

Forums

Organizing posts per day by dynamically changing showposts (5 posts)

  1. buzink
    Member
    Posted 4 years ago #

    I would like my posts on the front page to be organized per day (like issues of a magazine). I came up with the code below.

    It works on the first page of the front page, it only shows posts from the last issue (last day that posts were posted). If I click on 'older entries' things start getting to go wrong.

    The post-limit of the first page (day/issue) is used, instead of the limit of the page (day/issue) shown. That's because I base the date on '$posts[0]->post_date', which is the first post in the posts object. That object holds always all latest posts, also on front pages two and beyond.

    To fix my problem I need a way to determine the array-number of the post with which the frontpages 2 and beyond starts their loop, so I can use that to get the date of the proper post. Like '$posts[18]->post_date' for example, where '18' is the array number.

    <?php // change post limit to show posts of only one day
    
    	if (is_front_page()) {
    
    		//get date of first post
       		$cpd=mysql2date('Ymd', $posts[0]->post_date);
    
    		//get all posts from that date
    		$posts_of_day=get_posts('year=' .substr($cpd, 0, 4) .'&monthnum=' .substr($cpd, 4, 2) .'&day=' .substr($cpd, 6, 2).'&showposts=1000');
    
    		//Count the amount of posts of that date
    		$daylimit=count($posts_of_day);
    
    		//limit the amount of posts that are shown accordingly
    		query_posts($query_string . "&showposts=".$daylimit);
    
    	} ?>
  2. tomontoast
    Member
    Posted 4 years ago #

    I see your problem. I think you may need to use a custom query to get the number of posts published on each day.
    You can find out which page you are on using:
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    Altogether:

    $query = "SELECT DISTINCT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month', DAYOFMONTH(post_date) AS 'dayofmonth', count(ID) as posts FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC";
    $results = $wpdb->get_results($query);
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $dayresult = $results[$paged];
    query_posts('year='.$arcresult->year.'monthnum='.$arcresult->month.'day='.$arcresult->dayofmonth);

    Completely untested but the general idea should work with a bit of modification for errors.

  3. buzink
    Member
    Posted 4 years ago #

    hi Thomas,

    Thanks for thinking with me! I don't understand the paging part of your code. Also, it doesn't seem to have the desired effect. The pages use the showlimit set in the wp preferences now.

    Do you know how wordpress knows from which array-number in the posts object it has to start the loop in frontpage 2 en beyond? There must be some variable I can call.

  4. tomontoast
    Member
    Posted 4 years ago #

    Do you know how wordpress knows from which array-number in the posts object it has to start the loop in frontpage 2 en beyond? There must be some variable I can call.

    WordPress doesn't do this. It actually uses a LIMIT 10 OFFSET (n*x) type clause when querying the database where n is page number and x is posts per page. On the other hand query_posts has no limit or offset unless you define it.

    Also, it doesn't seem to have the desired effect. The pages use the showlimit set in the wp preferences now.

    Try:

    $query = "SELECT DISTINCT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month', DAYOFMONTH(post_date) AS 'dayofmonth', count(ID) as posts FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC";
    $results = $wpdb->get_results($query);
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $dayresult = $results[$paged];
    query_posts('showposts=1000&year='.$arcresult->year.'&monthnum='.$arcresult->month.'&day='.$arcresult->dayofmonth);

    I've changed this line query_posts('showposts=1000&year='.$arcresult->year.'&monthnum='.$arcresult->month.'&day='.$arcresult->dayofmonth); due to forgetting ampersands and setting showposts to 1000

  5. tomontoast
    Member
    Posted 4 years ago #

    If I get time I'll have a tinker with the code myself on my own server.

Topic Closed

This topic has been closed to new replies.

About this Topic