I am trying to get a post ID by it’s post content. I have a custom post type of ‘membership’ which has post_content that looks like this…

{"member_id":33}

I am trying to get the ID of this post by searching for {“member_id”:33}

Am I best off doing this using an SQL statement or is there a better way to do it using WordPress functions?

1
1

WordPress has a handy function called post_exists() which allows you to find an existing post by its title, content, and/or date. It returns the post ID on success or 0 otherwise.

In your case, you can use $post_id = post_exists( '', '{"member_id":33}' ) to find the post you’re looking for.

Note: That function doesn’t check the post type so you’d need to do that separately or roll your own function if it’s necessary. I doubt it though that you have other posts with the same content 🙂

Leave a Reply

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