Solved

Get posts for last month, build links for previous months

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
archiving old posts 9 43
Wordpress CLI 4 36
Help with a header and phone number hyperlink 2 14
How can I increase the size of this logo 5 24
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…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
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…

863 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

16 Experts available now in Live!

Get 1:1 Help Now