4

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.

5
  • 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. Commented Feb 24, 2016 at 12:52
  • I am testing my own post exist in the wordpress, I thought builtin function is more appropriate for the job
    – justnajm
    Commented Feb 24, 2016 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 Commented Feb 24, 2016 at 13:06
  • I want to check the post with title and content already exists ?
    – justnajm
    Commented Feb 24, 2016 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
    Commented Feb 25, 2016 at 6:25

2 Answers 2

19

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' );
}
3
  • 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
    Commented Feb 25, 2016 at 6:30
  • What's an alternative for checking a post exists by title, without requiring post.php? Commented Feb 1, 2019 at 11:24
  • Either will have to write own code for it, else using existing admin function with in function_exists call
    – justnajm
    Commented Feb 27, 2019 at 12:22
1

You may have used the wrong hook.

I had this problem when using:

function my_create_pages() {
    
    if ( !post_exists( "My Title" ) ) {
    
        // Create post
    }
}
add_filter('init', 'my_create_pages');

Solution was to use the admin_init hook:

function my_create_pages() {
    
    if ( !post_exists( "My Title" ) ) {
    
        // Create post
    }
}
add_filter('admin_init', 'my_create_pages');

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.