0

So I created a transient object that used get_posts() to get a series of posts based on some argument. Well the transient was stored - it was created - YAY - now I tried to write this loop:

public function transient_loop($transient_name){
    global $post;
    if(false !== get_transient($transient_name)){
        foreach(get_transient($transient_name) as $post){
            setup_postdata($post);
            $this->_loop_content();
        }
    }
}

This looks for the transient name, if it exists, loops over it - sets up the post data and then from there calls a method that goes through and does typical loop stuff, like images, title, links and more - all your typical jazz,

Now what came back was: Notice: Trying to get property of non-object in /var/www/wordpress/wp-includes/query.php on line 3645

The stack trace sais the last thing called was the setup_postdata($post) so I var dumped $post and got int 1

Not sure if that's normal. So I decided to var_dump the transient - maybe a post or set of posts is not coming through at all.

Note: I am using xdebug - thats why the object is all formatted nicely. But I did: var_dump($transient_name) and got back:

object(WP_Post)[259]
  public 'ID' => int 1
  public 'post_author' => string '1' (length=1)
  public 'post_date' => string '2013-08-16 22:57:23' (length=19)
  public 'post_date_gmt' => string '2013-08-16 22:57:23' (length=19)
  public 'post_content' => string 'Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!' (length=86)
  public 'post_title' => string 'Hello world!' (length=12)
  public 'post_excerpt' => string '' (length=0)
  public 'post_status' => string 'publish' (length=7)
  public 'comment_status' => string 'open' (length=4)
  public 'ping_status' => string 'open' (length=4)
  public 'post_password' => string '' (length=0)
  public 'post_name' => string 'hello-world' (length=11)
  public 'to_ping' => string '' (length=0)
  public 'pinged' => string '' (length=0)
  public 'post_modified' => string '2013-08-16 22:57:23' (length=19)
  public 'post_modified_gmt' => string '2013-08-16 22:57:23' (length=19)
  public 'post_content_filtered' => string '' (length=0)
  public 'post_parent' => int 0
  public 'guid' => string 'http://localhost/wordpress/?p=1' (length=31)
  public 'menu_order' => int 0
  public 'post_type' => string 'post' (length=4)
  public 'post_mime_type' => string '' (length=0)
  public 'comment_count' => string '1' (length=1)
  public 'filter' => string 'raw' (length=3)
  public 'format_content' => null

So what's with the error?

  • So $transient_name is a post object? the function get_transient aspect a string as argument – gmazzap Sep 8 '13 at 13:02
0

What is the code on line 3645? You didn't specify.

From what I see in your code, $transient_name should be something like "_my_post_list_transient" - a string.

If $transient_name is returning a post object, then your call to this function may be the problem.

  • $transient_name is pretty simple to see what it does. It gives you the sting of the name of the transient. So if you have a transient called "bobs_posts_on_dead_people" that's what get's passed into the function. Its get_transient($transient_name) that returns the post object ... Any ways, as for 3645 - I don't know - that's WordPress core. I assumed some one here would know this error from dealing with transients... I can go searching for it and post it if you would like .. – TheWebs Sep 7 '13 at 22:16
  • Apologies, I misread the function name. I see that $transient_name is supposed to have the name, but where you var_dump it, you get back an OBJECT not a string. setup_postdata($post) is expecting to be passed a post object, not the integer that you've given it. I still suspect that your $transient_name is not the string you think it is coming into this function. – Jeff Rose Sep 7 '13 at 22:28
  • Jeff, I am var dumping get_transient($transient_name) this function RETURNS you a post object OR false. thats why your suppose to be doing if(false === get_transient('some_name')){}else{} My code is very similar to this: wpbeginner.com/wp-tutorials/… – TheWebs Sep 7 '13 at 22:42
  • You should update the original question then. In that you state "var_dump($transient_name)" brings back a post object. If your question isn't accurate, no answer can help. – Jeff Rose Sep 8 '13 at 1:43
0

the correct way to do this is not fail at spelling, its get_posts() not get_post

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.