I am trying to display multiple post based off a custom metabox selection. I am currently using CMB2 as my framework to display the CMB within the backend. The selection field which has an ID of work__assign is calling all post titles from another CPT called fleet. Below is my code for displaying the CMB2 select type which then displays the titles from a post-type fleet.
$work->add_field( array(
'name' => 'Assign',
'desc' => 'assign a vehicle',
'id' => $prefix . '_assign',
'type' => 'select',
'options' => get_fleettype_options('fleettype'),
));
//ASSIGN A FLEET
function get_fleettype_options($a) {
$args = array(
'post_type' => 'fleet',
'orderby' => 'ID',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => -1 // this will retrive all the post that is published
);
$result = new WP_Query( $args );
$title_list[''] = "Select a Vehicle";
if ( $result-> have_posts() ) :
while ( $result->have_posts() ) : $result->the_post();
$title_list[get_the_ID()] = get_the_title();
endwhile;
endif;
wp_reset_postdata();
return $title_list;
}
On the single page of fleet I am trying to display my CPT (work_order)by only showing the ones I have selected. For example work orders 2, 3 and 4 are assigned to Fleet 1. When I go to the single page for Fleet 1 I would like to list all work orders that are assigned to that fleet. Currently my below code is listing all CPT Work Order instead of the selected one
<?php
$custom = get_post_custom();
$work_post_id = $custom["work__assign"][0];
$args = array(
'post_type' => 'work_order',
'p' => $work_post_id,
'orderby' => 'ASC',
'posts_per_page' => -99,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>
Display info
<?php endwhile; wp_reset_postdata(); endif; ?>
What am I missing or doing wrong with trying to display post based off a what is being selected.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I got it to work by using the following code
function get_gas_options($a) {
$args = array(
'post_type' => 'fleet',
'orderby' => 'ID',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => -1 // this will retrive all the post that is published
);
$result = new WP_Query( $args );
$title_list[''] = "Assign a Vehicle";
if ( $result-> have_posts() ) :
while ( $result->have_posts() ) : $result->the_post();
$title_list[get_the_ID()] = get_the_title();
endwhile;
endif;
wp_reset_postdata();
return $title_list;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0