简体   繁体   English

如何使用 WP_Query 获取 WooCommerce 产品名称?

[英]How to Get WooCommerce Product Name with WP_Query?

I am trying to use this code on "Wp All Import".我正在尝试在“Wp All Import”上使用此代码。 If there is a product name in the database, that product should be omitted, but the code will not work as is.如果数据库中有产品名称,则应省略该产品,但代码不会按原样工作。 What do I need to do for the code to work?我需要做什么才能使代码正常工作?

add_filter('wp_all_import_is_post_to_create', 'create_only_if_unique_custom_field', 10, 3);
function create_only_if_unique_custom_field( $continue_import, $data, $import_id ) {
    // Only run for import ID 1.
    if ( $import_id == 33 || $import_id == 34 ) {

        // The custom field to check.
        $key_to_look_up = "post_title";

        // The value to check where 'num' is the element name.
        $value_to_look_up = $data['name'];

        // Prepare the WP_Query arguments
        $args = array (

            // Set the post type being imported.
            'post_type'  => array( 'post' ),

            // Check our custom field for our value.
            'meta_query' => array(array(
            'key'        => $key_to_look_up,
            'value'      => $value_to_look_up,
            )),
        );

    // Run the query and do not create post if custom field value is duplicated.
    $query = new WP_Query( $args );
    return !($query->have_posts());

    } else {

       // Take no action if a different import ID is running.
       return $continue_import;

    }
}

This variable controls the title.此变量控制标题。

  // Xml file column name
  $value = = $data['product_title'];
  
  // Get wpdb product title
  $posts = get_posts([
    'post_type'  => 'product',
    'title' => $value,  
  ]);
    

You can do like this.你可以这样做。

<?php

$params = array('posts_per_page' => 5); 
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) :
                $wc_query->the_post();  ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata();?>
<?php else:  ?>
<p>
     <?php _e( 'No Products' ); ?>
</p>
<?php endif; ?>

There are some ways to show the different types of Woocommerce product names with WP_Query.有一些方法可以使用 WP_Query 显示不同类型的 Woocommerce 产品名称。

 <?php
    //Pulling WooCommerce Products instead of WordPress Posts, Use this param
    $params = array(
            'posts_per_page' => 5,
            'post_type' => 'product'
    );
    
    //Displaying products of a given price range, use this param
     $params = array(
          'posts_per_page' => 100,
          'post_type' => array('product', 'product_variation'),
          'meta_query' => array(
          'relation' => 'OR',
           array(
               'key' => '_price',
               'value' => 5,
               'compare' => '<=',
               'type' => 'NUMERIC'
             ),
           array(
              'key' => '_sales_price',
              'value' => 5,
              'compare' => '<=',
              'type' => 'NUMERIC'
           )
       )
   );

  //Displaying available products only, use this param
   $params = array(
        'posts_per_page' => 5,
        'post_type' => array('product', 'product_variation'),
        'meta_query' => array(
         array(
               'key' => '_price',
              'value' => 5,
              'compare' => '<',
              'type' => 'NUMERIC'
             ),
          array(
             'key' => '_stock_status',
             'value' => 'instock'
         )
     )
 );

    
    $wc_query = new WP_Query($params); 

   if ($wc_query->have_posts()) : 
       while ($wc_query->have_posts()) : 
          $wc_query->the_post(); 
          the_title(); 
      endwhile;
   endif;

Also will help you the article https://www.gavick.com/blog/wp_query-woocommerce-products Thank you也将帮助您的文章https://www.gavick.com/blog/wp_query-woocommerce-products谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM