Solved

Get posts for last month, build links for previous months

Posted on 2013-10-31
1
260 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
Comment Utility
<?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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
In Part I (http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_8410-Getting-Started-In-WordPress-Part-I.html), I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now