3

I am using the function in my own class as I am using other wordpress functions in it.

I am unable to understand why it is not able to find the function while ide "go to declaration" point me to file wp-admin/includes/post.php

is wp-admin files functions do not exist as global.

  • 2
    You are most probably trying to use it on the front end, which would explain the error. Back end functions are not available on the front end. What are you trying to achieve here. As I said (in a now deleted comment), this is a scope issue. – Pieter Goosen Feb 24 '16 at 12:52
  • I am testing my own post exist in the wordpress, I thought builtin function is more appropriate for the job – justnajm Feb 24 '16 at 13:03
  • 3
    Why not just use get_post(), if it returns the post, it exists, if not, it does not exist. Otherwise, copy the post_exists function to your theme and rename it, then you can use it in the front end – Pieter Goosen Feb 24 '16 at 13:06
  • I want to check the post with title and content already exists ? – justnajm Feb 24 '16 at 13:16
  • I find the best solution is to copy the post_exists function in my own class, thanks @PieterGoosen, While this function was also suppose to be declared at global level – justnajm Feb 25 '16 at 6:25
8

The files in wp-admin are only loaded when you're in the admin area... when you're looking at pages or posts those functions aren't loaded. In that case you'd need to require the file first, so you'd want to do something like this in your function:

if ( ! is_admin() ) {
    require_once( ABSPATH . 'wp-admin/includes/post.php' );
}

The if ( ! is_admin() ) part is important because WordPress includes the file automatically when is_admin() is true so you only want to include it if is_admin() is false. Also make sure to include this snippet before you try to call the post_exists() function.

If that doesn't work then try this:

if ( ! function_exists( 'post_exists' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/post.php' );
}
  • 1
    Thanks :), I don't think its a good idea to include all the functions defined there for checking same title and content post exist – justnajm Feb 25 '16 at 6:30
  • What's an alternative for checking a post exists by title, without requiring post.php? – Robert Andrews Feb 1 at 11:24
  • Either will have to write own code for it, else using existing admin function with in function_exists call – justnajm Feb 27 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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