ACF Query Posts by Repeater Field Not Empty

This question seems to go unanswered on the Internet, perhaps because it is not possible. I just want to request all messages in which the repeater field has strings. i.e.

$args = array( 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'repeater_field', 'value' => '', 'compare' => '=!' ) ); 

I know that the alternative is to run the statements inside the loop to filter them out, but this contradicts some other logic, which is based on the number of messages in the request.

Does anyone have any thoughts on this?

+5
source share
3 answers

Suppose you have a repeater field labeled My Repeater Field , and this repeater contains at least one field labeled Field A on the repeater .

Assuming you have the default wp_ table prefix, you need to look at the wp_postmeta DB table to notice that the values ​​for this field in the relay are stored using meta_key :

NAME_OF_THE_REPEATER_index_NAME_OF_FIELD_ON_THE_REPEATER

So, in our case, if the message has 3 lines in the repeater field, its values ​​will be saved as:

 my_repeater_field_0_a_field_on_the_repeater my_repeater_field_1_a_field_on_the_repeater my_repeater_field_2_a_field_on_the_repeater 

Knowing this, if you want to request all messages that have at least one line on the repeater, you can do:

 $meta_query = [ [ 'key' => 'my_repeater_field_0_a_field_on_the_repeater', 'compare' => 'EXISTS', ] ]; $args = [ 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => $meta_query, 'post_type' => 'post', 'post_status' => 'publish', ]; $posts_array = get_posts( $args ); 

Note. As stated in WP Docs , you can use the EXISTS comparison for WP> = 3.5, and you do not need to specify a value when using the "EXISTS" or "NOT EXISTS" comparisons in WordPress 3.9 and later. I also assume that you are using PHP> = 5.4 to use the syntax of short arrays. If not, just replace [] with array() .

+3
source

You can query the Wordpress database using the $ wpdb object. ACF fields are stored in prod_postmeta in the database, so you will run the request. Your meta_value will be the key of your repeater field, so make sure you replace it in the request below. All keys for any ACF field starting with the field, and then random characters / numbers, will follow, as shown below. Then, when you have the message identifier, you can run get_post () for these message identifiers. Let me know if you need anything else or if you have questions.

 global $wpdb; $results = $wpdb->get_results("SELECT post_id from prod_postmeta WHERE meta_value = 'field_534eeaaa74199'"); $echo $results; 
+1
source

It works. I checked it out. Only by mail "Welcome to the world" it does not work.

 $args = array( 'post_type'=> 'post', 'posts_per_page' => -1, 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'repeater_field', 'value' => '0', 'compare' => '!=' ) )); $the_query = new WP_Query( $args ); 

"Repeat field" is the name of the field, not key_field. Here is the row count.

0
source

Source: https://habr.com/ru/post/1258163/


All Articles