WordPress.org

Ready to get started?Download WordPress

Forums

Convert code to WP 2.6 (2 posts)

  1. DrHallows
    Member
    Posted 6 years ago #

    Hello all,

    I want to use the Google News Sitemap Plugin but is not working in 2.6, because i need to change a line of code.

    function get_category_keywords($newsID)
    {
    global $wpdb;
    $categories = $wpdb->get_results("SELECT category_id FROM $wpdb->post2cat WHERE post_id=$newsID");
    $i = 0;
    $categoryKeywords = "";
    foreach ($categories as $category)
    {
    if ($i>0){$categoryKeywords.= ", ";} //Comma seperator
    $categoryKeywords.= get_catname($category->category_id); //ammed string
    $i++;
    }
    return $categoryKeywords; //Return post category names as keywords
    }

    function write_google_news_sitemap()
    {

    global $wpdb;
    // 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

    // Begin urlset
    $xmlOutput = "<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'");

    //Limit to last 3 days, 1000 items
    $rows = $wpdb->get_results("SELECT ID, post_date_gmt
    FROM $wpdb->posts
    WHERE post_status='publish' AND (DATEDIFF(CURDATE(), post_date_gmt)<=3)
    ORDER BY post_date_gmt DESC
    LIMIT 0, 1000");

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

    //Use the categories for keywords
    $xmlOutput.= get_category_keywords($row->ID);

    $xmlOutput.= "</news:keywords>\n";
    $xmlOutput.= "\t\t</news:news>\n";
    $xmlOutput.= "\t</url>\n";
    }

    // End urlset
    $xmlOutput.= "</urlset>\n";
    $xmlOutput.= "<!-- Last build time: ".date("F j, Y, g:i a")."-->";

    $xmlFile = "../google-news-sitemap.xml";
    $fp = fopen($xmlFile, "w+"); // open the cache file "news-sitemap.xml" for writing
    fwrite($fp, $xmlOutput); // save the contents of output buffer to the file
    fclose($fp); // close the file
    }

    add_action('publish_post', 'write_google_news_sitemap');
    add_action('save_post', 'write_google_news_sitemap');
    add_action('delete_post', 'write_google_news_sitemap');
    ?>

    Any idea? Thanks!

  2. chris.jinks
    Member
    Posted 6 years ago #

    Hi DrHallows,

    I wrote the original script for this plugin and have recently updated it for all you people using the newer WordPress 2.3+ database taxonomy.

    Please try the latest version (v1.1) from here: http://wordpress.org/extend/plugins/google-news-sitemap-generator/

    I have plenty of other developments in the pipeline for this plugin, but this should suffice for now.

Topic Closed

This topic has been closed to new replies.

About this Topic