admin管理员组文章数量:1326278
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:
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/Function_Reference/add_meta_box
Share Improve this question edited Nov 4, 2013 at 12:57 SomeoneS asked Nov 4, 2013 at 12:10 SomeoneSSomeoneS 1251 gold badge1 silver badge7 bronze badges 2- 1 Which plugin did you use? Please edit your question and add a link. Sidenote: Questions are meant to be references for later visitors as well. Please rework your questions spelling/punctuation/grammar/capitalization so it as as easy to read as possible. Thanks. – kaiser Commented Nov 4, 2013 at 12:39
- I wanted to say that I used that code (on that link), in my own plugin. Never mind, deleted that part. – SomeoneS Commented Nov 4, 2013 at 12:58
4 Answers
Reset to default 7Just 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:
// args to query for your key
$args = array(
'post_type' => 'your_post_type',
'meta_query' => array(
array(
'key' => 'videoid',
'value' => $new_posts_videoid // for example: '111'
)
),
'fields' => 'ids'
);
// perform the query
$vid_query = new WP_Query( $args );
$vid_ids = $vid_query->posts;
// do something if the meta-key-value-pair exists in another post
if ( ! empty( $vid_ids ) ) {
// do your stuff
}
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.
// args to query for your key
$args = array(
'post_type' => 'YOUR_POST_TYPE',
'meta_query' => array(
array(
'key' => 'YOUR_META_FIELD_NAME',
'value' => '111'
)
),
'fields' => 'ids'
);
// perform the query
$query = new WP_Query( $args );
$duplicates = $query->posts;
// do something if the key-value-pair exists in another post
if ( ! empty( $duplicates ) ) {
// do your stuff
}
Or wrap it up in a function:
function meta_value_exists($your_meta_value) {
$args = array(
'post_type' => 'YOUR_POST_TYPE',
'post_status' => 'publish',
'numberposts' => 1,
'meta_key' => 'your_meta_field',
'meta_value' => $your_meta_value,
);
$current_post = get_posts($args);
if( $current_post ) {
return true;
} else {
return false;
}
}
So then you could just check on your meta value:
$video_id = 1234;
if(meta_value_exists($video_id){
// do something if exists
} else {
// do something if not exists
}
Found it:
$args = array(
'meta_query' => array(
array(
'key' => 'videoid',
'value' => $_POST['videoid']
)
)
);
$videoQuery = new WP_Query( $args );
if ( $videoQuery->have_posts() ) :
while ( $videoQuery->have_posts() ) :
$videoQuery->the_post(); ?>
echo "<h3 class='post-title'>" . the_title() . "</h3>";
endwhile;
endif;
本文标签: metaboxCheck if meta key value already exists
版权声明:本文标题:metabox - Check if meta key value already exists 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742213232a2434098.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论