admin管理员组文章数量:1426226
I am creating a recruitment plugin. There's a custom post type called opening, and it has a metabox which displays a select
field with a list of candidates(each option's value is the candidate's ID as a WP User).
Now, when the post is saved, the selected option
of the field is added as a meta field of the post. The issue is that I need to know the ID of the candidate and the ID of the recruiter who assigned the candidate to the opening.
I tried adding the meta field as opening-candidate-{$candidateID}-recruiter
, and assign the recruiterID as the row value, however, It doesn't feel right to insert dynamic named columns in the DB.
Example:
I'd appreciate any better ideas to handle this!
I am creating a recruitment plugin. There's a custom post type called opening, and it has a metabox which displays a select
field with a list of candidates(each option's value is the candidate's ID as a WP User).
Now, when the post is saved, the selected option
of the field is added as a meta field of the post. The issue is that I need to know the ID of the candidate and the ID of the recruiter who assigned the candidate to the opening.
I tried adding the meta field as opening-candidate-{$candidateID}-recruiter
, and assign the recruiterID as the row value, however, It doesn't feel right to insert dynamic named columns in the DB.
Example:
I'd appreciate any better ideas to handle this!
Share Improve this question asked Jun 13, 2019 at 8:57 HimadHimad 5552 silver badges9 bronze badges1 Answer
Reset to default 0A better approach would be to save two meta fields. One for the candidateID and one for the recruiter.
Then when you need to query the posts you should use the WP_Meta_Query with both meta fields and the relation argument set to 'AND':
$args = array(
'post_type' => 'opening',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'open_candidate',
'value' => $candidateID,
'compare' => '='
),
array(
'key' => 'recruiter',
'value' => $recruiterID,
'compare' => '='
)
)
);
$posts = new WP_Query( $args );
Or if you have the post and need to find the candidate and the recruiter, you can just use:
$candidateID = get_post_meta( $post_id, 'open_candidate', true );
$recruiter = get_post_meta( $post_id, 'recruiter', true );
This hasn't been tested but it should work.
Codex reference for WP_Meta_Query: https://codex.wordpress/Class_Reference/WP_Meta_Query
本文标签: pluginsHow to create meta fields with more than 1 relation
版权声明:本文标题:plugins - How to create meta fields with more than 1 relation? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745405884a2657248.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论