Solved

Get posts for last month, build links for previous months

Posted on 2013-10-31
1
262 Views
Last Modified: 2013-11-14
I am building a feed page for what is basically an online newsletter.  What I'd like is to show the posts in the feed by month, displaying only the current month's info.

I am having no problem building the feed to display the posts, but can't find where to set the date information for the layout.

Here's my current loop.

<?php
					$today = getdate();
					$args = array( 'numberposts' =>15 , 'category_name' => 'news' , 'post_date' = '$today');
					$lastposts = get_posts( $args );
					foreach($lastposts as $post) : setup_postdata($post); ?>
						<?php the_title(); ?>
            	
            		<?php endforeach; ?>

Open in new window


I am trying to use $getdate(); to find the current month but didn't see any options in the WP Codex to add the results of that to my array.

Additionally, I'd like to add links on the bottom of the page where the user can select a month, and it would then rebuild the feed changing the variable to the selected month.

Thanks!
0
Comment
Question by:axessJosh
1 Comment
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 500 total points
ID: 39616631
<?php
                                        $year = date("Y"); //4-digit year
                                        $month = date("n"); //month without leading zeros
					$args = array( 'numberposts' =>15 , 'category_name' => 'news' , 'year'=$year,'month'=$month);
					$lastposts = get_posts( $args );
					foreach($lastposts as $post) : setup_postdata($post); ?>
						<?php the_title(); ?>
            	
            		<?php endforeach; ?>
                                  

Open in new window


That should work for you.

You can use any parameter of wp_query with the get_posts function.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

WordPress has made it easy to create scalable sites based on their powerful CMS, but as great as WordPress is, there are some SEO issues that haven’t been addressed out of the box.  Fortunately these problems can be solved with a few plugins.  The f…
Do you think that WordPress is just for blogs?  Think again!  WordPress is really a fantastic all around platform that you can use to develop websites on.  Integrated into its basic functionality is the ability to create pages using your choice of a…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question