Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6780185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:28:32+00:00 2026-05-26T16:28:32+00:00

Im trying to get a list of posts from WordPress for the last 7,30

  • 0

Im trying to get a list of posts from WordPress for the last 7,30 and 365 days.

Here is the code im using:

$args = array(
        'posts_per_page'=>10,
        'post_status'=>'publish',
        'post_type'=>'post'
    );
    if(isset($_GET['group'])){
        switch($_GET['group']){
            case 'week':
                $time = date('Y-m-d h:i:s',strtotime('-1 week'));
                break;
            case 'month':
                $time = date('Y-m-d h:i:s',strtotime('-1 month'));
                break;
            case 'year':
                $time = date('Y-m-d h:i:s', strtotime('-1 year'));
                break;
            default:
                $time = '';
                break;
        }
        if($time!=''){
            $args['pub_date'] = '>= "'.$time.'"';
        }
    }

    $query = new WP_Query( $args );

If I error_log the args array I can see the timestamp being set. But when I error_log the WP_Query object I can see the SQL query:

SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1  AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value desc LIMIT 0, 10

This query does not hold the pub_date date range I have set. How can I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:28:33+00:00Added an answer on May 26, 2026 at 4:28 pm

    Credit goes to MichaelH in this Thread on wp.org forums. You could modify it, to fit your requirements.

    <?php
      function filter_where($where = '') {
        //posts in the last 30 days
        //$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        //posts  30 to 60 days old
        //$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
        //posts for March 1 to March 15, 2009
        $where .= " AND post_date >= '2009-03-01' AND post_date <= '2009-03-15'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get list of recent posts from a blog filtered by
I am trying to get a list of subdirectories in a given directory using
I'm trying to get a list of user's friends using fb connect for the
I'm trying to get a list of stored procedures in t-sql. I am using
I am trying to get the list of people from the http://en.wikipedia.org/wiki/Category:People_by_occupation . I
So I'm trying to create a simple list of posts from a certain category
I am trying to join a table and a list in LINQ. From posts
i am trying to get a list of names from MySQL table useing php
Im trying to get a list of line items to a webpage using JSON,
I am trying to get a list of shares on a specific machine. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.