WP to acf fields from the database?

// This:
// $ACF_repeater - Box repeater
$ACF_repeater = $wpdb->get_results("SELECT * FROM UZ2N3VC9r8_postmeta WHERE post_id = 9 AND meta_key = 'ACF_repeater'");

//$ACF_repeater[0]->meta_value - the number of iterati

// In the database field named ACF_repeater_0_field ACF_repeater_1_field ACF_repeater_2_field and so on.
// How to make a dynamic meta_key?
// Option as it is now written does not work
for ($i=0; $i < $ACF_repeater[0]->meta_value; ) {
$meta_key = 'ACF_repeater_' . (string) $i . '_field';
$delivery_volume = $wpdb->get_results("SELECT * FROM UZ2N3VC9r8_postmeta WHERE post_id = 9 AND meta_key = $meta_key");

March 23rd 20 at 19:09
2 answers
March 23rd 20 at 19:11
Execute a separate SQL query in each iteration of the loop - not healthy and not good for performance. Let's first get the number of elements in ripetere:
SELECT meta_value FROM wp_postmeta WHERE post_id=83 && meta_key='recipients'

The result:
array:1 [
 0 => [
 'meta_value' => '3'

Further, will form at the level of the PHP array of valid keys:
$total = (int) $results[0]['meta_value']; // Value in database in string
$keys = [];

for ( $i = 0; $i < $total; $i ++ ) {
 $keys[] = "recipients_{$i}_email";

dump( $keys );

The result:
array:3 [
 0 => "recipients_0_email"
 1 => "recipients_1_email"
 2 => "recipients_2_email"

And now you can do one query to get all values for these fields putting them in WHERE meta_key IN (). It turns out we have something like this:
$keys = implode(',', $keys); // putting the string IN
$query = "SELECT * FROM wp_postmeta WHERE post_id=83 AND meta_key IN ({$keys})";

The only thing that PHP will stick the array into a string without the quotes, and for IN need that the values be in quotes. Specifically, this query will give error. Don't know how you do implement queries, but PDO::quote() for example solves this problem. I think more will understand.
If the rules several times, so the end result with the text in the notifications is not the same. - sammy commented on March 23rd 20 at 19:14
Very thank you)) - rafaela.Hahn commented on March 23rd 20 at 19:17
March 23rd 20 at 19:13
Just get_fields($id) not tried? See an example in the documentation.
get_fields is not suitable. Because I want to display the content on a different subdomain - rafaela.Hahn commented on March 23rd 20 at 19:16
@rafaela.Hahn, attach it to the rest API to endpoint and get them from the front - teresa commented on March 23rd 20 at 19:19
@teresa, Sorry I do not understand you)
Actually, I wonder why meta_key = $meta_key does not work. I pass a string there
This -
$meta_key = 'ACF_repeater_0_field'
meta_key = $meta_key
And this is
meta_key = 'ACF_repeater_0_field'
the same thing, but the first option does not work - rafaela.Hahn commented on March 23rd 20 at 19:22
speaking of meta-keys... Note that if the meta key starts with underscore, it is private and just will not be available in the REST API. It will be necessary to filter and "allow" this meta key.
Dmitry suggested that you pick up a small rest service that when accessing a specific url of the second domain will return the data you want.
For example, you are making a get request to a certain url, get the JSON and render the data in the second domain. To do this, you must be familiar with the WP REST API. - Dock commented on March 23rd 20 at 19:25
@Dock, REST is certainly an option, but if both sites on a single server and have access to the same database directly on the same server, then use an IPA, IMHO, a very big overkill. 2 simple SQL query and a few lines of PHP code to solve the problem in this case is much faster and more efficient. - sammy commented on March 23rd 20 at 19:28

Find more questions by tags WordPress