0

Inside my functions.php I use get_post() or similar (such as get_the_title()) inside some of my functions. It does not return anything. Why? And how to fix that?

P.S: And I can not really add add_action('somehook', 'myfunc'), because that makes things display in the wrong place.

1
  • 1
    It would be better if you show your code, I'm certain you then would get a even more on point answer. Not to take anything away from @jgraup's one. Commented Nov 5, 2016 at 22:26

1 Answer 1

0

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.
1
  • Hey, apparently get_queried_object() was what I was looking for, thanks a ton! :) It returns the post. Now i only need a proper explanation why this worked ^^
    – Alex
    Commented Nov 5, 2016 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.