Short answer “no”, using globals is never a best practice, indicates that you might be trying to do something in the wrong place, and should be left as a last resort.
In your snippet for example you have no idea who set the $post
, was it set in the loop or outside it, was it a single post loop, etc.
As a rule of thumb never use globals, they are just evil.
Related Posts:
- Action hook ‘wp’ firing twice… why?
- Count singular post views automatically
- Custom function for “Submit for Review” hook
- How can I hook into creating a new post and execute wp_die(), before the post is inserted into the database?
- How can i do something after head like adding a hook for after head but before post
- Adding buttons to Add New Post and Add New Page
- Copy post to separate database with “add_action(….)”
- Call Web Services on post first publish
- What does WordPress do if I save a post without content/title? [duplicate]
- Divs appearing everywhere in post content
- Automatically set post_parent value
- Is there a way to know when a page has been updated and do some action only once?
- Is update_post_meta used when save_post action hook is invoked?
- Count singular post views automatically
- Grab meta data before post is saved
- Is there a action hook for the “Empty Trash” button?
- Function to execute when a post is moved to trash .
- add action only on post publish – not update
- Hook that get’s triggered when the author of a post is changed
- How to validate XML-RPC post creation and cancel when needed?
- The hook for the AJAX Add to Cart button?
- Get old values for post before saving new ones
- Execute function when post is published
- Create posts on user registration
- how to limit edit_form_after_title hook to page and post edit only?
- How to publish a post with empty title and empty content?
- How to check if which hook triggered the call to a function?
- Callback for wp.autosave.server.triggerSave();
- flush_rewrite_rules on save_post Does Not Work on First Post Save
- How to add a “publish” link to the quick actions
- In what sequence are the hooks fired when a post is “published”?
- Better post meta efficiency?
- Is it possible to create an action hook using do_action() within add_action()?
- How to change post status from publish to draft using hook in wordpress?
- Filter post before *editing*
- WordPress Delete hook with wp_delete_post function?
- Get the post_id of a new post
- First hook to use current page post id
- How to filter content post only on save
- how “manage_posts_custom_column” action hook relate to “manage_${post_type}_columns” filter hook?
- How to place HTML below the title of the (custom) post overview
- Does an action fire when adding a tag via the “Tags” meta box?
- add_action not using ‘delete_post’ action with wp_delete_post
- the_posts filter been called multiple time
- Get post id in wordpress action?
- Are posts updated or built from revisions + autosaves?
- How to run a function when post is edited or updated using publish post action?
- Create cron job without a plugin?
- How to generate numbers indistinguishable for the IDs of the posts
- Action hook for new pending posts?
- How to retrieve the postID in a “image_send_to_editor” hook function?
- Register post status, exclude from searches
- How to modify post content?
- Which WordPress hook fires after post content loaded?
- What is the filter or hook to add admin controls to posts on the front end?
- Adding custom fields to bbpress reply form
- use wpml_post_language_details function other plugin based on $post->ID [closed]
- Hide a specific category in admin All Posts page (WordPress)
- Updating post data on save (save_post vs wp_insert_post_data)
- Hooking into the post editing screen for an existing page only
- Trying to alter the post_content through the_post
- How to check if single.php has already called the_post_thumbnail function
- How to dynamically parse and transform post/page content before displaying it?
- admin_notices action doesn’t trigger within save_post action
- Adding bootstrap classes to video shortcodes
- preg_replace not removed “class”
- why the same code got different results when using query_posts in functions.php and index.php
- Modify posts listing at back end
- Prevent posts with certain post_meta to be edited
- How to catch wordpress post ID when it’s published
- How to check in functions.php if there is data in a WP_Query?
- Is it possible to add a shortcode below post title?
- Update post meta within save_post action
- Retrieve post in AJAX Callback
- Exists filter or action that change Add New Post link?
- What hook can I use to modify custom post data before it is displayed on the page?
- Actions to use when flushing cache (when posts are added / deleted / modified)
- Posting to WP via URL
- Restricting displayed posts to posts from only select authors
- How can I change the date format of the revision list?
- Run a function when a custom post is update?
- the wp_post_update isn’t working all the time
- Hook when post is set from published to draft?
- Send email to user when I publish a new post
- Get Category in save_post Hook
- How to prevent post update using transition_post_status
- How can i take all ids from untrash_post action?
- Can’t get post_updated hook to work
- Is it possible to completely replace a post with an action/filter?
- How to get the postID inside ‘content_save_pre’? Other hook?
- wp query custom orderby not custom field
- Hook To Get All Posts Deleted
- How to remove related post from home page
- Checking if some condition is matched before inserting post into database
- fire action when post is being edited as well as saved?
- Which hooks are essential for post templates?
- save_post not working
- Action while post is being published
- Do anything on post_status change [duplicate]
- Admin notice on wp_insert_post