1

[Solved -> see last comment in this question]

In custom post type CPT_A I am saving first_name and last_name via a custom metabox form field (they are not taxonomies):

update_post_meta( $post_id, 'first_name', esc_attr( $_POST['first_name'] ) )
update_post_meta( $post_id, 'last_name', esc_attr( $_POST['last_name'] ) )

In the database it looks like this:

post_id, meta_key, meta_value
186, first_name, John
186, last_name, Doe
323, first_name, Bill
323, last_name, Jones

In custom post type CPT_B I am trying to create a dropdown that is populated by CPT_A => first_name and last_name.

How do I loop through first and last name?

[Solved] With help form Gareth Gillman' post I removed the foreach loop, moved the <option> into the while loop and changed get_the_id() to $query1->post->ID.

Here is the working solution. If there is a more eloquent way please comment.

<select name="mailing_name" id="mailing_name">

<?php 

$select_array = array();

$args = array (
    'post_type' => 'fooBar',
);

$query1 = new WP_Query( $args );

while ( $query1->have_posts() ) {

    $query1->the_post(); 

    $first_name = get_post_meta( $query1->post->ID, 'first_name', true);
    $last_name = get_post_meta( $query1->post->ID, 'last_name', true);

    echo '<option value="">' . $first_name . '&nbsp;' . $last_name . '</option>';

}

wp_reset_postdata();
?>

</select>
  • Wouldn't it be simple to add it elements to an option while you update post meta for CPT_A so that you could get the array from the updated option? – Samuel Elh May 20 '16 at 23:49
  • Not following you by adding an option. Either way don't you have to get an array from CPT_A? Where I am confused is do I need to get two arrays one for first and one for last name and then loop both as the <option>. – Jason May 21 '16 at 0:01
  • I was only saying that, for better performance not querying the database for PT data every time, when you "save first_name and last_name via a custom metabox" you would insert these data into an option which you use later. No need for 2 arrays, just one with Two-dimensional array. e.g array( 186 => array( 'first_name' => 'John', 'last_name' => 'Doe' ), 323 => array( 'first_name' => 'Bill', 'last_name' => 'Jones' ) ) – Samuel Elh May 21 '16 at 0:06
  • Thats is a good idea, I will do that once I get a working dropdown. Tks. – Jason May 21 '16 at 1:01
1

You need to query the db using wp_query to get all the posts from the CPT, save the metabox contents to an array and then print the array.

Not tested but should work (i hope):

<?php
$select_array = array();
$args = array (
 'post_type' => 'your_cpt',
);
$query1 = new WP_Query( $args );
while ( $query1->have_posts() ) {
 $query1->the_post();
 $select_array[firstname] = get_post_meta(get_the_id(), 'the_meta_key', true);
$select_array[lastname] = get_post_meta(get_the_id(), 'the_meta_key', true);
}
wp_reset_postdata();
?>

<select name="mailing_name" id="mailing_name">
 <?php
 foreach ($select_array as $option) {
  echo '<option value="">'.$option[firstname].'&nbsp;'.$option[lastname].'</option>';
 }
 ?>
</select>
  • That looks very promising, I will give it a try right now. – Jason May 21 '16 at 0:02
  • That worked, but I am only getting the first character, i.e., for John Doe it's J D. – Jason May 21 '16 at 0:18
  • wierd, i would print the meta field before it's added to the array to see if the meta key is correct before it's added. – Gareth Gillman May 21 '16 at 0:20
  • Actually it's showing as J J and D D. The meta key is fine, but the $select_array is not right, see new comment in the question. – Jason May 21 '16 at 0:56
  • do a print_r($select_array); and show the results...... that will tell you if the array is fine... – Gareth Gillman May 21 '16 at 1:10

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.