Ideally, you would manage everything under a single, hierarchical post type. Different post types are completely unrelated to each other by design. You are working against their very nature by attempting to relate them.
That said, creating a relationship is not impossible. The approach depends on the anticipated relationships – one to one, one to many, many to one, just how many is “many”, and if reversing the relations is required. For example, you could setup a relationship table much like how taxonomy relationships are managed in the wp_term_relationships table. It’s essentially a list of post IDs and the corresponding terms. Instead of relating terms, you relate posts of other types.
Depending on your needs, something even simpler may be used, like storing relations in post meta. Or something more complex is also possible if need be.
Thread Starter
Quin
(@quin452)
Thanks.
This is what I am aiming for.
$pro_area_array = array();
$pro_area_args = array(
'post_type' => 'proarea',
'publish_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC'
);
$pro_area_query = new WP_Query($pro_area_args);
if($pro_area_query->have_posts()) {
$i = 0;
while($pro_area_query->have_posts()) {
$pro_area_query->the_post();
$pro_area_array += array($i => get_the_title());
$i++;
}
}
wp_reset_query();
$pro_areas = array();
foreach ($pro_area_array as $key => $value) {
$pro_areas[] = $value;
}
/*
$pro_areas = array(
'One',
'Two',
'Three'
);
*/
echo '<span><label for="pro-area">Professional Area: </label></span>';
echo '<select name="pro-area">';
foreach($pro_areas as $pro_area) {
if($pro_area == get_post_meta($post->ID, 'pro-area', true))
echo '<option selected>'. $pro_area .'</option>';
else
echo '<option>'. $pro_area .'</option>';
}
echo '</select>';
}
Now the dropdown is populated correctly, and using a “simple” array does as you expect, but whenever I try and use the actual array, the permalink is changed to the last pro-area.
EDIT: As soon as I try to pul in the pro-area posts, it screws things over, changing the permalink and not saving/selecting the option.
-
This reply was modified 6 years, 1 month ago by
Quin.
Thread Starter
Quin
(@quin452)
UPDATE: I have it fixed. The solution is here.