0

Imagine the following script:

<?php
namespace MySpace;

class MyClass {

  public static function execute() {
    // Do something
    post_exists(1,'','','my_post_type');
  } 

}
?>

I'm now calling this function at the top of the page template, before any HTML. The problem I get is that wp resolves the post_exists function to the namespace instead of falling back to the global scope, where wp functions are usually recognized in such cases.

So I actually get a PHP error saying Call to undefined function MySpace\post_exists(), why is that so? Is, at that moment, before the document head, the wp core not initiated, or something similar? Never experienced this bug so far...

2
  • Apparently the function can indeed not be used on the frontend in wp (see here), can anyone confirm this? Would be a huge time-saver if they could mention that little tiny detail in the docs.
    – DevelJoe
    Commented Apr 5, 2022 at 0:20
  • "can anyone confirm this" - see my answer.
    – Sally CJ
    Commented Apr 5, 2022 at 6:46

1 Answer 1

1

From your comment:

Apparently the function can indeed not be used on the frontend in wp (see here), can anyone confirm this?

I can confirm that the post_exists() function can be used on the front-end, but you need to manually load the file that defines the function, which is wp-admin/includes/post.php — see the "File: <path>" part in the "Source" section of the post_exists() documentation.

And the file has to be manually loaded because WordPress doesn't load it on the non-admin side of the site, or that the function is only loaded automatically in the wp-admin area.

So I actually get a PHP error saying Call to undefined function MySpace\post_exists(), why is that so?

I don't know for sure why, but logically thinking, it's because the calling function (MyClass::execute()) is in the MySpace namespace. So if it wasn't in a namespace, then the message would have not used "MySpace\", i.e. Call to undefined function post_exists().

The problem I get is that wp resolves the post_exists function to the namespace instead of falling back to the global scope

No, I don't think WordPress would not do that "resolves" thing, and what happened is likely this:

  1. PHP looked for a function named post_exists from the current namespace: MySpace\post_exists().
  2. PHP tried to find and call the global function post_exists().
  3. Neither found, and so PHP threw the (fatal) error in question.

See the (7th) rule here which says, "For unqualified names, if no import rule applies and the name refers to a function or constant and the code is outside the global namespace, the name is resolved at runtime".

Additional Notes

  1. The 1st parameter for post_exists() is the post title, but it seemed that you've passed an ID (1) instead. And if so, then you could just use get_post() to check whether the post exists.

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.