0

I'm trying to create a custom RSS feed that will list and display information from a custom table I've created, that I use elsewhere in my WP site. The table and everythign work fine.

However, even when I try to use a simple RSS template (which is set up and browseable):

<?php /* Template Name: Custom RSS Template - beforeAfters  */
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>
<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/"
>
<channel>
<?php
global $wpdb;
$beforeAfter = $wpdb->get_row('SELECT * FROM wp_before_after WHERE `gallery_display` = 1');
foreach($beforeAfter as $imageSet) { ?>
<item>
<title><?php echo get_the_title($imageSet->ID); ?></title>
</item>
<?php } ?>
</channel>
</rss>

I get this error:

This page contains the following errors: error on line 11 at column 51: Entity 'trade' not defined Below is a rendering of the page up to the first error.

A blank page is displayed (because nothing is rendered). However, if I remove the line where $beforeAfter is set, the error disappears and I begin to see the output of the RSS feed in a browser (although it displays errors about the foreach line being invalid.. which is fine.

But my question is, why is the $wpdb call, causing the error message, and how do I get the variable to pull the database row working so I Can use the variables within the RSS template?

** DUMP of $beforeAfter**

<pre class="xdebug-var-dump" dir="ltr">
<small>
C:\wamp64\www\bellavou\wp-content\themes\bellavou\rss-beforeAfters.php:17:
</small>
<b>object</b>
(
<i>stdClass</i>
)[
<i>4196</i>
]
<i>public</i>
'id'
<font color="#888a85">=></font>
<small>string</small>
<font color="#cc0000">'1'</font>
<i>(length=1)</i>
<i>public</i>
'created'
<font color="#888a85">=></font>
<font color="#3465a4">null</font>
<i>public</i>
'before_date'
<font color="#888a85">=></font>
<font color="#3465a4">null</font>
<i>public</i>
'after_date'
<font color="#888a85">=></font>
<font color="#3465a4">null</font>
<i>public</i>
'patientID'
<font color="#888a85">=></font>
<small>string</small>
<font color="#cc0000">'2137'</font>
<i>(length=4)</i>
<i>public</i>
'procedureID'
<font color="#888a85">=></font>
<small>string</small>
<font color="#cc0000">'238'</font>
<i>(length=3)</i>
<i>public</i>
'patient_display'
<font color="#888a85">=></font>
<small>string</small>
<font color="#cc0000">'1'</font>
<i>(length=1)</i>
<i>public</i>
'procedure_display'
<font color="#888a85">=></font>
<small>string</small>
<font color="#cc0000">'1'</font>
<i>(length=1)</i>
<i>public</i>
'gallery_display'
<font color="#888a85">=></font>
<small>string</small>
<font color="#cc0000">'1'</font>
<i>(length=1)</i>
<i>public</i>
'before_img'
<font color="#888a85">=></font>
<small>string</small>
<font color="#cc0000">'face-lift_1_b.jpg'</font>
<i>(length=17)</i>
<i>public</i>
'after_img'
<font color="#888a85">=></font>
<small>string</small>
<font color="#cc0000">'face-lift_1_a.jpg'</font>
<i>(length=17)</i>
<i>public</i>
'period_taken'
<font color="#888a85">=></font>
<small>string</small>
<font color="#cc0000">'1week'</font>
<i>(length=5)</i>
<i>public</i>
'notes'
<font color="#888a85">=></font>
<small>string</small>
<font color="#cc0000">''</font>
<i>(length=0)</i>
</pre>
  • Just after the $wpdb call can you dump $beforeAfter for us? – Andy Macaulay-Brook Jul 25 '16 at 15:46
  • There's a lot there, I think pasting it as text might be a bit too much, a screenshot might be more helpful to read it... – Lee Jul 25 '16 at 15:47
  • Text is better, how much is "a lot"? Or see if there is a record within it with "trade" in. – Andy Macaulay-Brook Jul 25 '16 at 15:48
  • No value of 'trade' was found, apart from the error message. So WPDB is getting connected then. – Lee Jul 25 '16 at 15:50
  • It was $beforeAfter I was after rather than the error in your browser. I'm assuming that the call to $wpdb is OK. You know it returns an object and you are looping as though it's an array? Try removing your foreach loop a moment and see if you still get an error. – Andy Macaulay-Brook Jul 25 '16 at 16:00
1

After discussing we've discovered the problem isn't directly with calling $wpdb->get_row but a mismatch between the data it returns and the processing in the foreach loop.

For a single row of data, replacing...

foreach($beforeAfter as $imageSet) { ?>
    <item>
        <title><?php echo get_the_title($imageSet->ID); ?></title>
    </item>
<?php }

... with ...

<item>
    <title><?php echo get_the_title($imageSet->ID); ?></title>
</item>

... stops the feed erroring.

Or for multiple rows you could adjust your query.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.