1

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.

3
  • Although it's certainly possible to do what you want using custom meta-data, it could be simpler to instead use a taxonomy (fleet type) for the CPT of work_order. Then all you'd need is a template file to customize the display of all CPTs in the work_order that are tagged with the appropriate fleet_type in your custom taxonomy. There is a lot of documentation on how to register custom taxonomies for custom post types, if you decide to try that and get stuck post back and I'll give some examples.
    – Trisha
    Commented Jul 28, 2020 at 19:42
  • @Trisha I do agree having it setup as a taxonomy would be the simpler solution and I have thought about it. But with what I am trying achieve having a taxonomy wouldn't be the best solution. Have you ever come across something like what I am trying to do?
    – bigant841
    Commented Jul 28, 2020 at 19:51
  • Yes, I believe so, but I used custom taxonomy and terms.....on a site I did for a non-profit, we built a CPT for their "staff", with a custom taxonomy called "staff_categories" that held terms like "Board Member", "Volunteer", etc. Then I have a custom template file to display those Staff members separated by their role within the organization.....it sounds like what you're trying to do is similar, to a) create a work order, then b) assign it to a 'user', based on c) a custom meta-data field that holds the fleet type. If I understand it, I still think a custom taxonomy/term would be easier.
    – Trisha
    Commented Jul 29, 2020 at 20:09

1 Answer 1

0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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