WordPress.org

Forums

[resolved] Trying to pull last 30 day posts (12 posts)

  1. peiqinglong
    Member
    Posted 5 years ago #

    Greetings! So I'm trying to pull all posts for this month by using a date I enter into the Custom Fields. I think I'm very close, but I just need a tiny bit of help. Here's my code that I use in my sidebar:

    <?php
    mysql_connect(localhost, root, noxalaction);
    mysql_select_db(wpmu);
    
    $query = mysql_query("SELECT * FROM wp_posts,wp_postmeta WHERE wp_posts.id = wp_postmeta.post_id AND meta_key = 'Case Law Date' AND post_status = 'publish' AND post_type = 'post' ORDER BY meta_key LIMIT 0,5");
    
    while($r = mysql_fetch_array($query)) {
    $postdate = $r["meta_key"];
    $unixPostdate = strtotime($postdate);
    $mondaydate = strtotime('Last Monday');
    if(date('M')=="") $mondaydate = strtotime('Today');
    if($unixPostdate>$mondaydate) {
    echo "<a href=\"".$r["guid"]."\">".$r["post_title"]."</a>";
    }; }; ?>

    I found this code from this post and can't figure out how to adapt it for month. Any help is great appreciated!

  2. MichaelH
    Member
    Posted 5 years ago #

    Another method:

    <?php
    //assumes custom field has format of YYYY-MM-DD (e.g. 2010-03-05 for March 5 2010)
    $format_of_date = 'Y-m-d';
    $oldest = date($format_of_date, strtotime('-30 days'));
    $custom_field_key = 'Case Law Date';
    $args=array(
      'meta_key'=> $custom_field_key,
      'meta_value'=> $oldest,
      'meta_compare'=> '>=',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts where custom field ' . $custom_field_key . ' is on or after ' . $oldest ;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. peiqinglong
    Member
    Posted 5 years ago #

    Michael: Thanks for the speedy reply! I'm going to try this and report back :)

  4. peiqinglong
    Member
    Posted 5 years ago #

    Michael: is it possible to make it so that rather than 30-days it's just posts from whatever the current month is or is that vastly more difficult?

    Also if I wanted to say do last month's, I tried this:

    $oldest = date($format_of_date, strtotime('-30 days,-30 days'));

    But that didn't work for me.

  5. MichaelH
    Member
    Posted 5 years ago #

    You mean if it is March only show March posts? If it is April only show April posts?

  6. peiqinglong
    Member
    Posted 5 years ago #

    Michael: Yes!

  7. MichaelH
    Member
    Posted 5 years ago #

    <?php
    //assumes custom field has format of YYYY-MM-DD (e.g. 2010-03-05 for March 5 2010)
    $days_since_first_of_month = date('d')-1;
    $format_of_date = 'Y-m-d';
    $oldest = date($format_of_date, strtotime('-' . $days_since_first_of_month .' days'));
    $custom_field_key = 'Case Law Date';
    $args=array(
      'meta_key'=> $custom_field_key,
      'meta_value'=> $oldest,
      'meta_compare'=> '>=',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts where custom field ' . $custom_field_key . ' is on or after ' . $oldest ;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  8. Jonas Grumby
    Member
    Posted 5 years ago #

    Also:

    <?php
    //based on Austin Matzko's code from wp-hackers email list
      function filter_where($where = '') {
        //posts in the last 30 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>
  9. MichaelH
    Member
    Posted 5 years ago #

    SS_Minnow - that filter is for the post date, not for a date used in a custom field.

  10. Jonas Grumby
    Member
    Posted 5 years ago #

    Ok thanks. I just remembered seeing it as a query to pull posts from the past 30 days. I didn't really read it thoroughly.

  11. peiqinglong
    Member
    Posted 5 years ago #

    Michael: you're awesome! Do you have a link where I can buy you some coffee or something?

  12. MichaelH
    Member
    Posted 5 years ago #

    Your thanks is payment enough. Good luck.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.