最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to create meta fields with more than 1 relation?

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

A 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

发布评论

评论列表(0)

  1. 暂无评论