There are records in wordpress, in the records there are fields like:

price_meta_key_1 == 1200 price_meta_key_2 == 1500 price_meta_key_3 == 2000 price_meta_key_... 

Each field contains a numeric value - price.

How to make a filter of all entries " ΠžΡ‚ ...β‚½ Π”ΠΎ ...β‚½ " for this data!?

Each entry has a Meta Box " Information " with additional fields.

How to write a request WP_Query{} with the filter on the amount with VAT ?

enter image description here

    1 answer 1

    1. If you need to find records that satisfy the interval from .. to, where at least one of the information fields with the VAT amount (metal, fittings, etc.) satisfy the condition, then use relation => OR if all the fields should be subject to conditions from before we use AND.

       $args = array( 'posts_per_page' => -1, // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ всС посты 'post_type' => 'product', // Ρ‚ΠΈΠΏ постов 'meta_query' => array( 'relation' => 'OR', // OR/AND Π² зависимости ΠΎΡ‚ Π»ΠΎΠ³ΠΈΠΊΠΈ array( 'key' => 'price_meta_key_1', // сумма с НДБ ΠΏΠΎ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ ΠΈΡ‰Π΅ΠΌ 'value' => array(1000, 2500), // Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π² ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΊΠ΅ ΠΎΡ‚-Π΄ΠΎ 'type' => 'numeric', 'compare' => 'BETWEEN', ), array( 'key' => 'price_meta_key_2', 'value' => array(1000, 2500), 'type' => 'numeric', 'compare' => 'BETWEEN', ), array( 'key' => 'price_meta_key_3', 'value' => array(1000, 2500), 'type' => 'numeric', 'compare' => 'BETWEEN', ), ), ); $query = new WP_Query($args); 
    2. Solution from a response using REGEXP in a meta query: https://wordpress.stackexchange.com/questions/193791/use-regexp-in-wp-query-meta-query-key#answer-193841

    3. Select all meta-keys with price_meta_key_, create arrays and connect in meta_query:

        global $wpdb; $results = $wpdb->get_results( " SELECT meta_key FROM {$wpdb->prefix}postmeta WHERE meta_key LIKE 'price_meta_key_%' ", ARRAY_A ); $results = array_unique(array_map(function($v) {return $v['meta_key'];}, $results)); foreach ($results as $value) { $myquery[] = array( 'key' => $value, 'value' => array(1000, 2500), // Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π² ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΊΠ΅ ΠΎΡ‚-Π΄ΠΎ 'type' => 'numeric', 'compare' => 'BETWEEN', ); } // Ρ„ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ массив всСх Π½Π°ΠΉΠ΄Π΅Π½Π½Ρ‹Ρ… ΠΊΠ»ΡŽΡ‡Π΅ΠΉ для ΠΊΠ²Π΅Ρ€ΠΈ $args = [ 'posts_per_page' => -1, // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ всС посты 'post_type' => 'product', // Ρ‚ΠΈΠΏ постов 'meta_query' => array_merge($myquery, array( 'relation' => 'OR', // OR/AND Π² зависимости ΠΎΡ‚ Π»ΠΎΠ³ΠΈΠΊΠΈ )), ]; $query = new WP_Query($args); 
    • one
      @ Daniel, it seems like a solution with REGEXP should work - haven't you tried it? - KAGG Design
    • one
      @ Daniel what - no? Does not work or did not try? What documentation is needed for a response in SO using REGEXP? There the full code is laid out. - KAGG Design
    • @Pavel The version with REGEXP works - "^ location [0-9]". Which of the prepositional options is more optimal? For speed and safety. - Daniel
    • @KAGGDesign Does the speed of the request affect the number of "^ location [0-9]" For example, if it is [0-40]? - Daniel
    • @ Daniel certainly affects the amount. Only regexp will not be a bad [0-40], it is not formed that way. Both options are suggested - by reference and here in the question, as a result they generate an identical mysql request (as far as I can see from reading the code). So, in terms of speed and safety, the same thing. - KAGG Design