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.


enter image description here

I'd appreciate any better ideas to handle this!


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',
      'key'     => 'open_candidate',
      'value'   => $candidateID,
      'compare' => '='
      '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.org/Class_Reference/WP_Meta_Query

  • When you want to make less SQL queries you just store a lot of information in the array and pull it from there when necessary. More SQL === more problems. – Max Yudin Jun 13 at 9:41
  • @Max Yudin Storing data as an array in a meta field would not allow you to query the posts by the stored data as that would be serialized. It is ok to store arrays for options etc. but not very useful for posts meta that might be needed in queries. – freejack Jun 13 at 10:46
  • An opening can have several candidates added by different recruiters, so I think this wouldn't work. – Himad Jun 13 at 10:49
  • You may need to give more details about your project so we can give you a better answer. In particular how do you need to work with the stored data? Do you need to run queries to find posts based on this data? – freejack Jun 13 at 10:52
  • For example, I have a taxonomy called 'stages'. It represents in which stage a candidate is on the opening process(CV sent, called for interview, etc..). The issue is the same, an opening can have several candidates and each candidate can have a stage. I made it work with the dynamic column name opening-candidate-{$candidateID}-stage, however, in case that one of the stages terms gets deleted, I have no simple way of deleting all the records associated with that term. – Himad Jun 13 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.