0

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'

1

1 Answer 1

1

This isn't a direct answer, as my solution was to implement this another way.

Instead of getting the values (array) from the homepages ACF field I added a date field to each of the pages I want to display. So, I query against that date (for each of those pages) instead of entering a list of pages on the Homepage for it to display.

            <?php
            global $post;
            $today = date("Ymd");
            $myposts = get_posts( array(
                'post_type' => 'liquidation',
                'posts_per_page' => 5,
                'meta_key' => 'active_till',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'meta_query' => array(
                    array(
                        'key'           => 'active_till',
                        'compare'       => '>=',
                        'value'         => $today,
                        'type'          => 'DATE',
                    ),
                ),
            ));

            if ( $myposts ): ?>

                    <?php foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
and so on...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.