Support » Plugins » Problem with global $post in widget

  • hategrinder


    Hello! I’m writing a widget which is based on the page it is displayed on. So if it is_single I use global $post and then can do all the stuff I need. But if another widget querying database like ‘recent posts’ is executed before my code, global $post contains the last queried post and not the one displayed on, that’s why my widget displays incorrect results. I tried to use global $wpdb and global $wp_query to resolve which post page is widget actually displaying on, but these seem to have the data related to the wrong one too. What should I do to always be able to get the post the widget is displayed on?

Viewing 2 replies - 1 through 2 (of 2 total)
  • whooami



    umm, well without spending a whole lotta time on this, why not use another variable, ie if youre only using the widget on single post pages, use something like $mywidget = $post or whatever (within single.php), make it a global, and then dont use $post in your widget, use the other variable.



    ok, thanks I got the idea, though imho it could be better to add some action assigning $post to some other global variable instead of modifying template files

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem with global $post in widget’ is closed to new replies.