skip to Main Content

I am trying to use this code on "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;

    }
}

3

Answers


  1. Chosen as BEST ANSWER

    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,  
      ]);
        
    

  2. 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; ?>
    
    Login or Signup to reply.
  3. There are some ways to show the different types of Woocommerce product names with WP_Query.

     <?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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search