0

How can I search in database if id exist in a post type?. I want to search $id=123 in post type 'movies' like this, is this the correct way to do it?

   $content = get_posts('post_type=movies&id=$id');

    if ($content)
            echo "post id exist";

        else
            echo "post id doesn't exist";
1

You can use:

echo 'movie' === get_post_type( 123 ) ? 'Yes' : 'No'; 

But if you need to check it many times in your theme, you might consider defining your own helper function:

if( ! function_exists( 'is_movie' ) )
{
    function is_movie( $mixed = null )
    {
        return 'movie' === get_post_type( $mixed );
    }
}

It might return true when $mixed is empty, \WP_Post object or a post ID.

So if you want to know if there exists a movie with ID 123:

echo is_movie( 123 ) ? 'Yes' : 'No';

or just is_move() within the loop, to check the current post ID.

If you only need to check if the ID 123 exists, then just use get_post().

For example:

echo is_null( get_post( 123 ) ) ? 'No' : 'Yes';
| improve this answer | |

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.