When $post
is null for get_post
it looks to $GLOBALS['post']
.
There are several other checks in the source so perhaps you should find out the value of the global variable when you’re calling it or give it something besides and empty value.
What does get_the_id()
return vs. get_post(get_queried_object_id())
? Or var_dump(get_queried_object())
?
get_post()
– Retrieves post data given a post ID or post object.
get_queried_object()
– Retrieve the currently-queried object. For example:
- if you’re on a single post, it will return the post object
- if you’re on a page, it will return the page object
- if you’re on an archive page, it will return the post type object
- if you’re on a category archive, it will return the category object
- if you’re on an author archive, it will return the author object
etc.
Related Posts:
- Post via wp-admin and via iOS app, same hooks and triggers involved?
- Is there a hook that runs after a user logs in?
- Difference between do_action and add_action
- Why do some hooks not work inside class context?
- Difference between after_setup_theme and init action hooks?
- Trigger custom action when setting button pressed
- Please explain how these hooks work
- Hook after image is uploaded and image sizes generated
- add_action(‘wp_ajax_[action name]’, myfunction) problem
- What is the earliest possible hook for safely using `is_front_page`?
- How to check if which hook triggered the call to a function?
- Send data to 3rd party api with wp_remote_post on wp_login
- Run javascript code after wp_login hook?
- Disable block from editor based on post type
- Difference between hooks Plugin_loaded and admin_int?
- Redirect logged in users if they are on a specific page
- admin_post hook not called
- Check if action hook exists before adding actions to it
- Hooks are not executing
- How to debug removal of rewrite rule flushing?
- after login that will redirect user role into a page
- add_action on inherit post status
- Detect type of post status transition
- Create hooks based on an array of hook names?
- Access post meta just after publishing
- How to pass arguments to add_action() [duplicate]
- action lifecycle
- How to use the post_updated hook before and after arguments
- Is there a recover_post hook to go with trash_post hook?
- Redirect users on specific post category or category page
- WP Admin Bar frontend issue with dashicon deregister
- How to get post ID in post_updated action hook?
- add_action for lost_password or modify wp-login.php?action=lostpassword
- Run add_action hook if condition
- Add my own function to existing WooCommerce hook
- What is the best filter where to use register_block_type?
- How to use do_action_ref_array?
- Check if do_action(‘custom_action’) is hooked into?
- How to find hooks as per Just-In-Time approach?
- how to determine how many and what kind of arguments are passed to hooks
- WP CLI Get all Enqueued Scripts and Styles
- Delay an action until current action is completed
- Where to hook my plugin’s action
- Update post meta using pending_to_publish hook
- What’s hook to use immediately after a user is authentcated [duplicate]
- Hooking into the init action will fire it too frequently?
- Event-Driven Pattern vs MVC?
- How to pass arguments to add_action() or retrieve return value of called function?
- Implement Hooks Using Array
- Looking for a hook for post.php
- Save User Meta Email Address in Lowercase
- How do I trigger a post update within a get_posts() foreach loop?
- Hook when editing user
- get_post_metadata causing some meta data to fail
- Execute JavaScript in WordPress Hook
- Add action hook into wp_localize_script
- How to replace a function using a child theme?
- How do I prevent term from being created on create_term hook?
- Is it possible to add the_content filter upon login?
- WordPress wp_loaded action hook
- I would like to send a notification email (Asana) whenever something is published (posts, pages, custom post types) [duplicate]
- Add Different Actions To Different Page’s wp_footer
- add_action hook for publish_post not working
- How to run a function after wp() in the wp-blog-header.php file?
- Hook for inserting?
- Notify admin when Custom post meta data gets updated or deletet
- Remove genesis_404 hook from genesis_loop [closed]
- Add html code in admin page
- Action hook with wrapper html
- What hook to add at start of WordPress load
- Adding custom Bulk Actions
- What hook to use to redirect based on $post
- Change status of page after an event (Looking for best practice advice)
- Can we change the hook firing sequence?
- Session management issues with WordPRess 404 Error page
- How to call do_action() to pass arguments to hooked functions that take different number of args?
- Using publish_{custom-post-type} hook for custom post type to update meta doesn’t work
- Struggling with plugin dev basics: add_action
- RTrouble passing arguments to action
- How to hook add_action() into after category description with id?
- Check if front page within function passed to action
- How can I insert custom html code inside a div dynamically?
- How to trigger click events using hooks
- Grab meta data before post is saved
- $new_pass always returns null – password_reset hook
- How to call get_post()?
- Object oriented programming, add_action in constructor not firing
- When is get_currentuserinfo() needed?
- How do I remove an action hook by s2member
- Removing action from template class
- Which hook should I use for this scenario regarding the registration process and account/profile update?
- Function is Missing an Action Hook
- frontend show edit profile with selected custom options
- Redirect back to origin page after using get_delete_post_link()
- Create action running on trashed_post hook to modify post_meta value
- The acction hook stop working if i move it from plugin file to theme’s functions.php file
- admin_post hook not working
- add_action failed to display function by a plugin
- How do I set a custom post type Category after import using wp_set_post_terms
- Hooks to trigger a callback when adding, removing, rearranging or updating a widget in the widget area