Adding Site Name and Featured Image to Global Posts Feed

I got help from Paul writing a couple queries and to get the site name and featured image, but for some reason they are not displaying within the feed content. I did a bit of clean up to close out the query, but am not quite sure how to get them to show up. Here’s my loop, anyone have any guidance?

if ( count( $posts ) > 0 ) {
foreach ($posts as $post) {
$author_display_name = $wpdb->get_var("SELECT display_name FROM " . $wpdb->base_prefix . "users WHERE ID = '" . $post['post_author'] . "'");
$blog_name = get_blog_option($post['blog_id'], ‘blogname’);
switch_to_blog( $post['blog_id'] );
$featured_image_id = get_post_meta($post['ID'], ‘_thumbnail_id’, true);
if ($featured_image_id) {$image_src = wp_get_attachment_image($featured_image_id, array(100, 100));}
?>
<item>
<title><?php echo apply_filters( 'the_title_rss', $post['post_title']); ?></title>
<link><?php echo $post['post_permalink']; ?></link>
<comments><?php echo $post['post_permalink'] . '#comments'; ?></comments>
<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', $post['post_published_gmt'], false); ?></pubDate>
<dc:creator><?php echo $author_display_name; ?></dc:creator>
<guid isPermaLink="false"><?php echo $post['post_permalink']; ?></guid>
<description><![CDATA[<?php echo apply_filters('the_excerpt_rss', wp_trim_excerpt($post['post_content'])); ?>]]></description>
<content:encoded><![CDATA[<?php echo $blog_name; ?><?php echo $image_src; ?><?php echo apply_filters('the_content_feed', $post['post_content'], 'rss2'); ?>]]></content:encoded>
<wfw:commentRss><?php echo $post['post_permalink'] . 'feed/'; ?></wfw:commentRss>
</item>
<?php
}
}

  • Timothy
    • Chief Pigeon

    Hey there.

    Are you getting any errors there?

    For the Blog name try changing this:

    $blog_name = get_blog_option($post['blog_id'], ‘blogname’);

    To:

    $blog_name = get_blog_option(get_current_blog_id(), 'blogname');

    You could also try swapping out:

    $post['ID']

    With:

    get_the_ID()

    Works for me.

    Let me know how that goes for you.

    Take care.

  • jakelisby
    • Design Lord, Child of Thor

    Ok, that got the blog name to show up in there, but I’m still not getting the featured image.

    I’m not getting any errors either, and checking the logs of the server, there’s no issues. It’s just not showing for some reason…

    Thanks for your help so far, we’re so close…

    if ( count( $posts ) > 0 ) {
    foreach ($posts as $post) {
    $author_display_name = $wpdb->get_var("SELECT display_name FROM " . $wpdb->base_prefix . "users WHERE ID = '" . $post['post_author'] . "'");
    $blog_name = get_blog_option(get_current_blog_id(), 'blogname');
    switch_to_blog( $post['blog_id'] );
    $featured_image_id = get_post_meta(get_the_ID(), ‘_thumbnail_id’, true);
    if ($featured_image_id) {$image_src = wp_get_attachment_image($featured_image_id, array(100, 100));}
    ?>
    <item>
    <title><?php echo apply_filters( 'the_title_rss', $post['post_title']); ?></title>
    <link><?php echo $post['post_permalink']; ?></link>
    <comments><?php echo $post['post_permalink'] . '#comments'; ?></comments>
    <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', $post['post_published_gmt'], false); ?></pubDate>
    <dc:creator><?php echo $author_display_name; ?></dc:creator>
    <guid isPermaLink="false"><?php echo $post['post_permalink']; ?></guid>
    <description><![CDATA[<?php echo apply_filters('the_excerpt_rss', wp_trim_excerpt($post['post_content'])); ?>]]></description>
    <content:encoded><![CDATA[<span id="schoolName"><?php echo $blog_name; ?></span> <?php echo $image_src; ?> <?php echo apply_filters('the_content_feed', $post['post_content'], 'rss2'); ?>]]></content:encoded>
    <wfw:commentRss><?php echo $post['post_permalink'] . 'feed/'; ?></wfw:commentRss>
    </item>
    <?php
    }
    }

  • jakelisby
    • Design Lord, Child of Thor

    @Barry helped me realize the previous version of Post Indexer doesn’t index featured images. The beta 3.0 does though. So now I have that up, and for some reason it’s still not indexing. The site name is also pulling the root site name for all the posts… Not quite sure what funkiness is going on there. Any thoughts?

  • jakelisby
    • Design Lord, Child of Thor

    Yup, I ran the index rebuild. I’m not 100% sure if my code to display the image is correct, so it could be indexing the image and I’m just not inserting it into the content node correctly. Do you have thoughts on how would be best to insert that?

  • jakelisby
    • Design Lord, Child of Thor

    Apparently it doesn’t want me to post that file type… Sorry, but here is all the code…

    <?php
    /*
    Plugin Name: Recent Global Posts Feed
    Plugin URI:
    Description: RSS2 feed showing global posts - to access feed go to http://yoursite.com/feed/globalpostsfeed
    Version: 3.0 RC 1
    Author: Barry (Incsub)
    Author URI:
    WDP ID: 70
    Network: true
    */

    /*
    Copyright 2012 Incsub (http://incsub.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    */

    class globalpostsfeed {

    var $build = 1;

    var $db;

    function __construct() {

    global $wpdb;

    $this->db =& $wpdb;

    if($this->db->blogid == 1) {
    // Only add the feed for the main site
    add_action('init', array(&$this, 'initialise_global_posts_feed') );
    }

    }

    function globalpostsfeed() {
    $this->__construct();
    }

    function initialise_global_posts_feed() {

    global $wp_rewrite;

    $installed = get_option('globalpostsfeed_version', false);

    add_feed('globalpostsfeed', array( &$this, 'do_global_posts_feed' ));

    if($installed === false || $installed < $this->build) {
    // We need to flush our rewrites so that the new feed is added and recognised
    flush_rewrite_rules();
    update_option('globalpostsfeed_version', $this->build);
    }

    }

    function do_global_posts_feed() {

    global $network_query, $network_post;

    // Remove all excerpt more filters
    remove_all_filters('excerpt_more');

    @header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
    $more = 1;

    echo '<?xml version="1.0" encoding="' . get_option('blog_charset') . '"?' . '>';

    $number = $_GET['number'];
    if ( empty( $number ) ) {
    $number = '25';
    }

    $posttype = $_GET['posttype'];
    if(empty($posttype)) {
    $posttype = 'post';
    }

    $network_query = network_query_posts( array( 'post_type' => $posttype, 'posts_per_page' => $number ));
    $blog_name = get_blog_option(get_current_blog_id(), 'blogname');
    switch_to_blog( $post['blog_id'] );
    $featured_image_id = get_post_meta(get_the_ID(), ‘_thumbnail_id’, true);
    if ($featured_image_id) {$image_src = wp_get_attachment_image($featured_image_id, array(100, 100));}

    ?>
    <rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    <?php do_action('rss2_ns'); ?>
    >

    <channel>
    <title><?php bloginfo_rss('name'); _e(' - Recent Global Posts','postindexer'); ?></title>
    <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
    <link><?php bloginfo_rss('url') ?></link>
    <description><?php bloginfo_rss("description") ?></description>
    <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', network_get_lastpostmodified('GMT'), false); ?></lastBuildDate>
    <language><?php bloginfo_rss( 'language' ); ?></language>
    <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
    <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
    <?php do_action('rss2_head'); ?>
    <?php while( network_have_posts()) : network_the_post(); ?>
    <item>
    <title><?php network_the_title_rss(); ?></title>
    <link><?php network_the_permalink_rss(); ?></link>
    <comments><?php network_comments_link_feed(); ?></comments>
    <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', network_get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
    <dc:creator><?php network_the_author(); ?></dc:creator>
    <?php network_the_category_rss('rss2'); ?>

    <guid isPermaLink="false"><?php network_the_guid(); ?></guid>
    <?php if (get_option('rss_use_excerpt')) { ?>
    <description><![CDATA[<?php network_the_excerpt_rss(); ?>]]></description>
    <?php } else { ?>
    <description><![CDATA[<?php network_the_excerpt_rss() ?>]]></description>
    <?php if ( strlen( $network_post->post_content ) > 0 ) { ?>
    <content:encoded><![CDATA[schoolName="<?php echo $blog_name; ?>" <?php echo $image_src; ?> <?php network_the_content_feed('rss2'); ?>]]></content:encoded>
    <?php } else { ?>
    <content:encoded><![CDATA[schoolName="<?php echo $blog_name; ?>" <?php echo $image_src; ?> <?php network_the_excerpt_rss(); ?>]]></content:encoded>
    <?php } ?>
    <?php } ?>
    <wfw:commentRss><?php echo esc_url( network_get_post_comments_feed_link(null, 'rss2') ); ?></wfw:commentRss>
    <slash:comments><?php echo network_get_comments_number(); ?></slash:comments>
    <?php network_rss_enclosure(); ?>
    <?php do_action('network_rss2_item'); ?>
    </item>
    <?php endwhile; ?>
    </channel>
    </rss>
    <?php
    }

    }

    $globalpostsfeed = new globalpostsfeed();

    ?>

  • Barry
    • DEV MAN’s Mascot

    You have to think “globally” now :slight_smile:

    So this part:

    $blog_name = get_blog_option(get_current_blog_id(), 'blogname');

    Will only get the id of the blog that’s running the feed, which is the main site.

    You need to change it to something like:

    $blog_name = get_blog_option( $network_post->BLOG_ID , 'blogname');

  • Barry
    • DEV MAN’s Mascot

    This bit:

    switch_to_blog( $post['blog_id'] );
    $featured_image_id = get_post_meta(get_the_ID(), ‘_thumbnail_id’, true);
    if ($featured_image_id) {
    $image_src = wp_get_attachment_image($featured_image_id, array(100, 100));
    }

    Should become something like:

    switch_to_blog( $network_post->BLOG_ID );
    $featured_image_id = get_post_meta(network_get_the_ID(), ‘_thumbnail_id’, true);
    if ($featured_image_id) {
    $image_src = wp_get_attachment_image($featured_image_id, array(100, 100));
    }
    restore_current_blog();

    Note: I just typed these into the forum post, so I haven’t checked for syntax errors – prob best to give them a quick scan first :slight_smile:

  • Barry
    • DEV MAN’s Mascot

    You might as well drop it in for now :slight_smile: My update will be to the post-indexer plugin to add that function in – btw if you want at any point to see the functions you can use, then they are in the post-indexer/includes/functions.php file.

  • jakelisby
    • Design Lord, Child of Thor

    @Barry I was bummed to miss your plugins and code chat today, but I think I realized what you were saying. So the code I dropped in is dependent on your update to the Post Indexer plugin.

    When do you think that new version would be ready? Is that something that takes days, weeks or months?

  • jakelisby
    • Design Lord, Child of Thor

    So here’s the query portion as I have it now. At this time it’s still displaying the root site’s Blog Name and not showing the Image src in the feed. All I’m really needing is the source url, so if I can get that in there, I’ll be good.

    $network_query = network_query_posts( array( 'post_type' => $posttype, 'posts_per_page' => $number ));
    $blog_name = get_blog_option( $network_post->BLOG_ID, 'blogname');
    switch_to_blog( $network_post->BLOG_ID );
    $featured_image_id = get_post_meta($network_post->ID, ‘_thumbnail_id’, true);
    if ($featured_image_id) {
    $image_src = wp_get_attachment_image($featured_image_id, array(100, 100));
    }
    restore_current_blog();

  • Imperative Ideas
    • HummingBird

    That’s a long way to go for a ham sandwich.

    Add this to your functions.php if you want the post thumbnail to show up in your RSS feed as part of the site description:

    /* include thumbnail in RSS feed */
    function add_thumb_to_RSS($content) {
    global $post;
    if ( has_post_thumbnail( $post->ID ) ){
    $content = '' . get_the_post_thumbnail( $post->ID, 'thumbnail' ) . '' . $content;
    }
    return $content;
    }
    add_filter('the_excerpt_rss', 'add_thumb_to_RSS');
    add_filter('the_content_feed', 'add_thumb_to_RSS');

  • jakelisby
    • Design Lord, Child of Thor

    @Barry I wanted to make sure to not lose sight of this. We’re limping along right now, but if you could let me know what else is needed to get this up and running that would make my life a lot less stressful. :slight_smile:

    Thanks for all your help with this.

  • jakelisby
    • Design Lord, Child of Thor

    Ok, we have this finished, thanks for all the help along the way.

    Replace your network_have_posts single line with this:

    <?php while( network_have_posts()) : network_the_post();
    switch_to_blog($network_post->BLOG_ID);

    //Get network blog post featured image
    $featured_image_id = get_post_thumbnail_id(network_get_the_ID());
    $featured_image_src = '';
    if ($featured_image_id) {
    $featured_image_src = array_shift(wp_get_attachment_image_src($featured_image_id, 'thumbnail'));
    }

    //Network blog name
    $blogname = get_blog_option($network_post->BLOG_ID, 'blogname');

    restore_current_blog();
    ?>

    Then you can use this to output the variables in your feed:

    <?php echo $blogname; ?>

    and

    <?php echo $featured_image_src; ?>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.