WordPress.org

Ready to get started?Download WordPress

Forums

Problem with global $post in widget (3 posts)

  1. hategrinder
    Member
    Posted 5 years ago #

    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?

  2. whooami
    Member
    Posted 5 years ago #

    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.

  3. hategrinder
    Member
    Posted 5 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic