10872

Exclude woocommerce products with no price set- wp query

Question:

I am trying to display the woocommerce products with almost similar regular price. For eg: Regular price of the current product is $1000

and some other products with prices are

<ol><li>product1 - $1001</li> <li>product2 - $1006</li> <li>product3 - $996</li> <li>product4 - $999</li> <li>product5 - $1003 </li> <li>product6 - $1001</li> <li>product7 - $1005</li> <li>product8 - $1010</li> <li>product9 - $998 </li> <li>product10 - $990</li> </ol>

Now if I was suppose to show 4 nearest products by price then they would be product 1,6,4,9

Because above seemed a bit difficult to me so I tried to get

<ul><li>two product with price greater than $1000 & </li> <li>two product with price greater than $1000 </li> </ul>

Here are the arguments for wp query that I tried

$args = array( 'post_type'=>'product', 'posts_per_page'=>2, 'order' => 'DESC', 'orderby' => 'meta_value', 'meta_key' => '_price', 'meta_query' => array( array( 'key' => '_price', 'value' => 1000, 'compare' => '<', 'type' => 'NUMERIC' ), ) );

I guess it should give me 2 nearest products with prices less 1000

But issue is it also gives me products for which the price is not set ..So I want to exclude the products with no price set..

Thanks in advance

Answer1:

You can use multiple meta queries, so you could add one to check if the price is higher then 0:

$args = array( 'post_type'=>'product', 'posts_per_page'=>2, 'order' => 'DESC', 'orderby' => 'meta_value', 'meta_key' => '_price', 'meta_query' => array( array( 'key' => '_price', 'value' => 1000, 'compare' => '<', 'type' => 'NUMERIC' ), array( 'key' => '_price', 'value' => 0, 'compare' => '>', 'type' => 'NUMERIC' ) ) );

You do not need to set the relation parameter, as it is set to "AND" by default.

Recommend

  • [mysql]SQL to retrieve tree data but some different order
  • google charts issue with annotation role :true make dynamic not working using dataview
  • Plotly - Create Pie Chart with continuous color scale
  • Display Table form with Eloquent Data Laravel
  • Read file line by line as soon as these lines appear in the file using Apache Camel
  • how does postgres handle the bit data type?
  • Indexing k-d tree?
  • WordPress MysqlError: Unknown storage engine 'InnoDB' [duplicate]
  • gulp.watch running same task multiple times when saving many files
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • Strong vs Weak entities MYSQL
  • Parenthesis() and SQL Query Performance
  • Cannot page through all results using nextPageToken on YouTube search API v3
  • unexpected behaviour of Google BigQuery WHERE NOT list CONTAINS string
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • Cannot get text from text area
  • MySQL performance when updating row with FK
  • NHibernate manually control fetching
  • How to 'create temp table as select' in Slick?
  • How to get data from **Realm database** using **date object**?
  • Do query loads all the data in memory
  • Merging rows to columns
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • Zurb Foundation _global.scss meta styles for js?
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Caching attributes in superclass
  • ActiveRecord query for a count of new users by day
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • $wpdb not working in file of WordPress plugin
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Get one-time binding to work for ng-if
  • Sending data from AppleScript to FileMaker records
  • Display Images one by one with next and previous functionality
  • How do you troubleshoot character encoding problems?
  • SQL merge duplicate rows and join values that are different
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • Django query for large number of relationships