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.

1 Answer
1

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' );
}

Leave a Reply

Your email address will not be published. Required fields are marked *