I have a Custom Post Type of 'ITEM' that allows the admin to add an 'Item Number' using an ACF text field.
In another Custom Post Type of 'GROUP' the admin can use an ACF Repeater to enter multiple 'Item Numbers' for 'ITEMS' to associate with this GROUP. I need the loop to show all posts that have an 'Item Number' that matches a number entered into the Repeater on the Group Page.
I'm using get_posts() to query on a GROUP page, to show all ITEM posts that match one of the numbers entered in the GROUP admin page.
It's failing. Whether I use meta_value in or meta_query.
First I have to flatten the array comping from the ACF repeater, as it presents a nested array. Then, I've had to convert that array to just values, so it can be passed to the 'value' in 'meta_query'.
Here is what I have so far:
// get the ACF field array
$arrays = get_field('included_inventory');
// outputs:
Array
(
[0] => Array
(
[item_number] => 5550
)
[1] => Array
(
[item_number] => 5551
)
[2] => Array
(
[item_number] => 5552
)
[3] => Array
(
[item_number] => 1234
)
)
// setup array
$result = array();
// flatten the array
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arrays));
// get the values
foreach ($it as $key => $value) {
$result[] = strval($value);
}
// outputs:
Array
(
[0] => 5550
[1] => 5551
[2] => 5552
[3] => 1234
)
// format the values
$results = "'" . implode("','", $result) . "'";
// outputs: '5550','5551','5552','1234'
$posts = get_posts( array(
'post_type' => 'liquidation',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'meta_key' => 'item_number',
'meta_query' => array(
array(
'key' => 'item_number',
'value' => array($results), // needs to be formatted as array('number','number','etc')
'compare' => 'IN'
)
)
));
I'm sure I'm just overlooking something simple.. but, I've been staring at this too long.. can't see passed it LOL.
I either get no posts or an error of 'Warning: trim() expects parameter 1 to be string'