Let’s say that I have few posts with meta key “videoid”. Value of that meta key is different for every post (and has to be different). When adding new post, how can I check if videoid with that exact value already exist for some older posts? I used this as a refference for adding custom meta box to admin area of WP: http://codex.wordpress.org/Function_Reference/add_meta_box
4 comments
Comments are closed.
Just do a query with
WP_Query
using the Custom Field parameters (meta_query
) to look for posts with the meta key and the value – exemplary code:There is no need to use
query_post()
– see:When should you use WP_Query vs query_posts() vs get_posts()?
. If you need a complete array of post objects, not just the ids, remove
'fields' => 'ids'
.ialocin’s answer incorrectly states that the wp_query stored as a variable would just spit out an array of ID’s. Instead it gives the whole WP_Query object, so you have to use ->posts to get that array of post ID’s.
Or wrap it up in a function:
So then you could just check on your meta value:
Found it: