TransWikia.com

Display a CPT based on a metabox selection

WordPress Development Asked by bigant841 on December 15, 2021

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.

One Answer

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

Answered by bigant841 on December 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP