无法通过自定义查询获取自定义邮政类型数据

时间:2022-01-19 作者:Hasnain Ghias

我有一个自定义分类法“;阴影类型“;其中包含两种类型

我正在查询DCM值以显示在WooCommerce选项卡中。我的代码很好,但结果中没有任何值。

以下是函数代码:

function d_woo_new_product_tab_content6($product) {
  // The DCM Tab Content
  global $product;
  // The Available tab content Here
  $args = array(
    \'post_type\' => \'colorshades\',
    \'fields\' => \'ids\',
    \'posts_per_page\' => -1,
    \'meta_query\' => array(
      array(
        \'key\'     => \'colors_product_id\',
        \'value\'   => $product->id,
        \'compare\' => \'=\',
      ),
    ),
    \'tax_query\'  => array(
      array(
          \'taxonomy\' => \'shade_type\',
          \'field\'    => \'slug\',
          \'terms\'    => \'dcm\'
      )
  )
  );
  $query = new WP_Query($args);
if ($query->have_posts()):
foreach( $query->posts as $id ):
//echo \'ID: \' . $id;
$Color_Code = get_post_meta($id, "color_code", true);
$R = get_post_meta($id, "red_code", true);
$G = get_post_meta($id, "green_code", true);
$B = get_post_meta($id, "blue_code", true);
$all_color_product_notification = \'\';
$image_url = site_url().\'/wp-content/uploads/2021/03/\'.rand(1,3).\'.png\';
echo \'<div class="colors_box mb-2">
<a href="javascript:;" class="colorshade" style="background:rgb(\'.$R.\',\'.$G.\',\'.$B.\');"  data-toggle="modal" data-target="#myModal2\'.$id.\'">
<img src="\'.$image_url.\'" class="img-responsive" style="width:100%; height:70px;">
</a>
</div>\';
?>
这是我的分类代码(Note: Taxonomy is working fine otherwise)

register_taxonomy(\'shade_type\',\'colorshades\',array(
    \'hierarchical\' => true,
    \'labels\' => array(
        \'name\' => _x( \'Shade Type \', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'Shade Type\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Search Shade Type\' ),
        \'popular_items\' => __( \'Popular Shade Type\' ),
        \'all_items\' => __( \'All Shade Type\' ),
        \'parent_item\' => null,
        \'parent_item_colon\' => null,
        \'edit_item\' => __( \'Edit Shade Type\' ), 
        \'update_item\' => __( \'Update Shade Type\' ),
        \'add_new_item\' => __( \'Add New Shade Type\' ),
        \'new_item_name\' => __( \'New Color Shade Type\' ),
        \'separate_items_with_commas\' => __( \'Separate Shade Type with commas\' ),
        \'add_or_remove_items\' => __( \'Add or remove Shade Type\' ),
        \'choose_from_most_used\' => __( \'Choose from the most used Shade Type\' )
    ),
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => false,
));
还附加以下分类屏幕截图:enter image description here

有人能帮我解决这个问题吗。谢谢

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

将我的查询更改为following对我很有用。我删除了产品ID字段;直接调用post分类法,因为我不需要产品Id。

 <?php 
  $args = array(
  \'post_type\' => \'colorshades\',
  \'fields\' => \'ids\',
  \'posts_per_page\' => -1,
   \'meta_query\' => array(
   array(
   \'key\'     => \'colors_product_id\',
    // \'value\'   => $product->id,
   // \'compare\' => \'=\',
                                        
   ),
   ),
   \'tax_query\'   => array(
    array(
    \'taxonomy\' => \'shade_type\',
    \'field\'    => \'slug\',
    \'terms\'    => \'dcm\'
      )
     )
   );
    $query = new WP_Query($args);