Support » Plugin: Post List » Current Post State Lost after Plugin Execution

  • I’ve been having troubles with this plugin at the time of accessing the state of the post or page in which it is located after the shortcode is processed.

    Placing this shortcode in a post or page in order to access a list of posts makes any further post information to be lost. I’ve found this to be very annoying in the case of the comment-status property of the posts: even though I disabled comments on the post I was using this plugin in, they kept appearing.

    After debugging the plugin, I found out that this is due to the way the list of posts is requested from the database. Once the list is obtained, it is iterated over the $post variable, which is global. The previous post information is overriden, and thus lost, for any later processing. The information stored in that variable from then on is the last post from the list that was obtained from the DB.

    I managed to fix this behavior up, and I’m telling you my small patch as an example in case it is useful for anyone else.

    In the plugin code, reference the global $post variable at the start of the postlist() function. Then, store the $post variable value into another temporary variable, i.e. $post2.

    Just close to the end of the function, after the post list is iterated and the $out variable is filled, but before it is returned, assign the $post2 value back to the global $post var. You’ll keep the original $post values for any further processing outside the plugin.

    In a nutshell:

    function postlist($attr){

    global $post; //MOVE THIS LINE UP
    $post2 = $post; //NEW LINE


    if ( $query->have_posts() ) :
    $out = ”

      while ( $query->have_posts() ) : $query->the_post();
      $out .= ”

    $post = $post2; //NEW LINE

    return $out;
    $post = $post2;
    return “<p>No Post found.</p>”;

    Finally, the “wp_reset_query();” line is never executed, I’m afraid.

    I hope this helps. Best Regards,


  • The topic ‘Current Post State Lost after Plugin Execution’ is closed to new replies.