1

I have a page where I want to show Blog Posts on a monthly basis. And I want to show the post of the previous month if the current month has no posts. Something like the image given below. It is showing the posts of August because September has no post. enter image description here

here the code I am grabbing current month post:

    <?php 
 $args = array('date_query' => array(
               'year' => date( 'Y' ),
               'monthnum' => date( 'm' ),
                ),
                    'posts_per_page' => 10,
                    'paged' => $paged,
                    'order' => 'ASC',
                    );
  $query = new WP_Query( $args );
  if ( $query->have_posts() ): while($query->have_posts()): $query->the_post(); ?>

                <h2>
                <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </h2>

                <p>
                    <?php the_content();?>
               </p>

<?php endwhile; else : ?>
                    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
               <?php endif; // Reset Post Data
              wp_reset_postdata();?>

but how can I show previous month posts, if the current month has no posts

2

First, get the current month and year:

$month = int(current_time('m'));
$year = int(current_time('Y'));

Next, get the posts in this month:

$query = new WP_Query( 'year=' . $year . '&monthnum=' . $month );

If the query returns empty query the month before and repeat this until you have found a non empty month:

while (empty($query)) {
  $month = $month-1;
  if ($month == 0) { $month = 12; $year=$year-1;}
  $query = new WP_Query( 'year=' . $year . '&monthnum=' . $month );
  }
| improve this answer | |
  • Thanks for the prompt help. I have added this code but finding an error. If you could help me with the full code, then I will use that one to see if that works. – Arafat Rahman Sep 16 '16 at 7:58
  • You'll have to be more specific than "an error" for me to tell you what might be wrong in your case. I cannot give more code that this if I do not know the code context in which you will be using it. – cjbj Sep 16 '16 at 8:13
  • here i used this code for grabbing current month post: – Arafat Rahman Sep 16 '16 at 10:04
  • This is really straightforward PHP, which you should be able to figure out yourself. You start with defining $month and $year and insert those in your $args. the you get $query and use my while-loop to get another month if needed. – cjbj Sep 16 '16 at 10:48
1

I have this code which does something similar but it's only counting posts. If you grab the required info in the SQL something like this should work...

global $wpdb;
$searchmonth = sprintf("%02s", absint($monthint));
$searchyear = 2016;

$found_posts = $wpdb -> get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_date LIKE '%-$searchmonth-%' AND post_date LIKE '$searchyear%' ");

if($found_posts){
      /* do something */
}else{

      if(1!=$monthint){
         $monthint--;
      }else{
         $monthint=12;
         $searchyear--;
      }
      /* get info for previous month */
}
$wpdb -> flush();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.