基于Metabox选择显示CPT

时间:2020-07-23 作者:bigant841

我正在尝试根据自定义元框选择显示多篇文章。我目前使用CMB2作为框架,在后端显示CMB。ID为work\\u assign的选择字段正在调用另一个名为fleet的CPT中的所有帖子标题。下面是我的代码,用于显示CMB2 select type,然后显示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;
}
在fleet的单个页面上,我试图通过仅显示我选择的CPT(work\\u order)来显示我的CPT(work\\u order)。例如,将工单2、3和4分配给车队1。当我转到震源组1的单个页面时,我想列出分配给该震源组的所有工单。目前,我下面的代码列出了所有CPT工作单,而不是所选的工作单

<?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; ?>
根据所选内容显示帖子时,我遗漏了什么或做错了什么。

1 个回复
最合适的回答,由SO网友:bigant841 整理而成

我使用以下代码使其工作

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