Display a CPT based on a metabox selection

Question

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 ti display post based off a what is being selected.

0
, , bigant841 1 year 2020-07-23T11:10:26-05:00 0 Answers 62 views 0

Leave an answer

Browse
Browse