WordPress.org

Support

Support » How-To and Troubleshooting » How do I get fetch_feed working with url from database

How do I get fetch_feed working with url from database

  • fetch_feed($url) seems only to be working with $url typed in manually. If I instead take it from the database (e.g. get_bookmark_field(‘link_rss’, $link->link_id); or $link->link_rss) it is not working anyhow (fatal error Call to undefined method WP_Error::get_items() )

    How do I get fetch_feed($url) working with an url form the database? It seems that there is a format in $url from database what fetch_feed($url) does not link.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Call to undefined method WP_Error::get_items()

    Seems whatever you’re doing to call the data from the database is failing, the error is not with fetch_feed, but with however you’re trying to get data from the database.

    Please have a short look to my function:

    function tp_get_bm() {<br />
    	global $wpdb;<br />
    	$queryString = "<br />
    		SELECT * FROM $wpdb->links<br />
    		WHERE link_visible = 'Y'<br />
    		 AND link_rss <>''<br />
    		Order by link_name";<br />
    	$my_links = $wpdb->get_results($queryString);<br />
    	include_once(ABSPATH . WPINC . '/feed.php');<br />
    	foreach ($my_links as $my_link) :<br />
    		$uri3= get_bookmark_field('link_rss', $my_link->link_id);<br />
    		echo $uri3;<br />
    		$uri1 = esc_attr($my_link->link_rss);<br />
    		echo $uri1;<br />
    		$uri2 = 'http://www.crazytoast.de/feed/';<br />
    		echo $uri2;<br />
    		$rss = fetch_feed($uri2);<br />
    		$rss_items = $rss->get_items( 0, $rss->get_item_quantity(1) );<br />
    	endforeach;<br />
    }

    It only works with $uri2. The echo of $uri1 and $uri3 results the same than $uri2. You can see it on my live testing side URL officetrend.de (left sidebar below tp bm).

    It seems to be a mistake in formating, but I do not now how should I format my URL coming from database so that fetch_feed does not have problems with it 😉

    Is it with particular URLs?

    I can test some code if you like, do you want me to test a particular feed?

    Maybe rather then doing this..

    $rss_items = $rss->get_items( 0, $rss->get_item_quantity(1) );

    You should be checking the what $rss has got back first (if anything) and go from there.. else you’re just assuming the feed was successful.. (it could be down, the url might not be valid, etc…).

    Before this line..

    $rss_items = $rss->get_items( 0, $rss->get_item_quantity(1) );

    Put the following..

    if( is_wp_error( $rss ) ) {
    	echo $rss->get_error_message();
    	continue;
    }

    It should tell you what the error is.. rather then just dying on the error.. (continue moves you along in the foreach loop, skipping the get_items … try it you’ll see what i mean..

    strange… now when I am going on with the function (including if is_wp_error else ) it works. No error anymore.
    Isn’t that strange? Without wp-error it doesnt work, with wp-error it works?

    What do you think is better for my $uri Use:
    – get_bookmark_field(‘link_rss’, $my_link->link_id);
    – esc_attr($my_link->link_rss);

    The only feed what still had an error was the blog.wordpress-deutschland.org/feed/, but this is because of default the wrong url is entered, as this feed has no trailing slash.

    What do you think is better for my $uri Use:

    No idea, whichever works best i guess..

    Not sure if you need the esc_attr as the data should of been sanitized going into the DB, i suppose there’s nothing wrong with being extra cautious though.

    Thank you very much for your help! 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How do I get fetch_feed working with url from database’ is closed to new replies.