Solved

Get posts for last month, build links for previous months

Posted on 2013-10-31
1
266 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to create your own WordPress Theme Before I begin with the theme design tutorial, I would like to give you the basics of what is required to make a WordPress Theme. The most basic WordPress theme only requires 2 files: a style.css file and an…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

733 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