WordPress.org

Forums

Wp db query show posts from last 3 days only (4 posts)

  1. cheaper
    Member
    Posted 5 years ago #

    Hi guys and girls

    Does anyone know how to amend the wpdb query so it will display the posts from the last 3 days only, for a google news feed..

    // Load WordPress configuration
    require("wp.travel.php");

    // Fetch options from database
    $permalink_structure = $wpdb->get_var("SELECT option_value FROM $wpdb->options
    WHERE option_name='permalink_structure'");
    $siteurl = $wpdb->get_var("SELECT option_value FROM $wpdb->options
    WHERE option_name='siteurl'");

    // Output XML header
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

    // Begin urlset
    echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"
    xmlns:news=\"http://www.google.com/schemas/sitemap-news/0.9\">\n";

    // Select all posts
    $rows = $wpdb->get_results("SELECT ID, post_date_gmt
    FROM $wpdb->posts WHERE post_status='publish'");

    // Output sitemap data
    foreach($rows as $row){
    echo "\t<url>\n";
    echo "\t\t<loc>";
    echo get_permalink($row->ID);
    echo "</loc>\n";
    echo "\t\t<news:news>\n";
    echo "\t\t<news:publication_date>";
    $thedate = substr($row->post_date_gmt, 0, 10);
    $thetime = substr($row->post_date_gmt, 11, 20);
    echo $thedate . 'T' . $thetime . 'Z';
    echo "</news:publication_date>\n";
    echo "\t\t<news:keywords>computers,internet</news:keywords>\n"; // change these keywords
    echo "\t\t</news:news>\n";
    echo "\t</url>\n";
    }

    // End urlset
    echo "</urlset>";
    ?>

    Thanks

    Kev

  2. MichaelH
    Member
    Posted 5 years ago #

    <?php
    $mylimit= 3 * 86400; //days * seconds per day
    $post_age = date('U') - mysql2date('U', $row->post_date_gmt);
    if ($post_age < $mylimit) {
    echo 'this post is within my date limit ';
    }
    ?>
  3. cheaper
    Member
    Posted 5 years ago #

    Thanks Michaelh but where do i insert the php in the code above

    Kev

  4. MichaelH
    Member
    Posted 5 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.