Search code examples
wordpressadvanced-custom-fields

How to pull rows from repeater table which contains value in select


I am trying to create a shortcode for custom posts I have on the WordPress site. Advanced custom fields plugin is used.

In the shortcode my itention is to pass car id, and bonus type. All cars have several bonuses in the table, but I want to pull just info from the row of bonus type. Either one or several bonus types could be applied.

[get_car_bonus carId="5,7,9,13" bonusType = "Welcome, 30-OFF"] [/get_car_bonus]

In the shortcode above, I pass 4 cars and I want to show for every car rows from repeater where bonus types are WELCOME and 30-OFF.

Here is how repeater table looks: repeater table

Is this possible? Any help is appreciated


Solution

  • OK here is short code example based on your question (i've removed spaces between commas in bonus type and change attribute name to bonusTypes)...

    [get_car_bonus carId="5,7,9,13" bonusTypes="Welcome,30-OFF"] [/get_car_bonus]

    This is not tested but maybe this might get you on the right track, see comments in my php code below so you know what is happening...

    // function that runs when shortcode is called
    function get_car_bonus_shortcode($atts) { 
      
        // store our passed shortcode attributes into var
        $atts = shortcode_atts([
            'carId' => false,
            'bonusTypes' => false
        ], $atts, 'get_car_bonus');
    
        // if either carId or bonusType is false return early
        if(!$atts['carId'] || !$atts['bonusTypes']) {
            return false;
        }
    
        // assuming your passed shortcode attribute values are correctly formatted comma serperated strings
        // lets explode them into arrays
    
        // explode car ids to integer array
        $cars = array_map('intval', explode(',', $atts['carId'));
    
        // explode bonus types to array
        $bonus_types = explode(',', $atts['bonusTypes');
    
        // now lets get our car posts from car ids (assuming your post type name is `cars`)
        $cars_query = new WP_Query([
            'post_type' => 'cars',
            'post__in' => $cars
        ]);
    
        // results data storage array
        $data = [];
    
        // if we have cars
        if($cars_query->have_posts() ) :
    
            // loop through each car found
            while ( $cars_query->have_posts() ) : $cars_query->the_post(); 
    
                // get repeater field rows in your car post
                $rows = get_field('your_repeater_field_name', $cars_query->post->ID, false);
    
                // if we have repeater rows
                if($rows) {
    
                    // now lets loop through each repeater row
                    foreach($rows as $row) {
    
                        // get repeater field bonus type select field value
                        // assuming your select field return type matches your passed shortcode bonus type strings
                        $bonus_type = $row['bonus_type'];
    
                        // if any of the passed attribute bonus types are equal to the current repeater field bonus type select field value
                        if(in_array($bonus_type, $bonus_types)) {
    
                            // now here is where we can store the current repeater row fields for matching bonus type to our data results array (by car id then bonus type)
                            $data[$cars_query->post->ID][$bonus_type] = [
                                'offer' => $row['offer'],
                                'bonus' => $row['bonus']
                            ];
    
                        }
                    }
    
                }
    
            }
    
            endwhile;
    
        endif;
    
        // if we have result data added to data array 
        if($data) {
    
            // print result data (see end of answer of how example data is returned)
            return '<pre>' .  print_r($data, true) . '</pre>';
    
        }
    
        // return false 
        return false;
        
    }
    
    // register shortcode
    add_shortcode('get_car_bonus', 'get_car_bonus_shortcode');
    

    Your question is a little vague on how you want to handle the final output. But if you run this shortcode below...

    [get_car_bonus carId="5,7,9,13" bonusTypes="Welcome,30-OFF"] [/get_car_bonus]

    Example returned output...

    Array
    (
        [5] => array
            (
                ['Welcome'] => array
                    (
                        ['offer'] => 'Offer repeater field example text'
                        ['bonus'] => 'Bonus repeater field example text'
                    )
                ['30-OFF'] => array
                    (
                        ['offer'] => 'Offer repeater field example text'
                        ['bonus'] => 'Bonus repeater field example text'
                    )
            )
        [7] => array
            (
                ['30-OFF'] => array
                    (
                        ['offer'] => 'Offer repeater field example text'
                        ['bonus'] => 'Bonus repeater field example text'
                    )
            )
        [9] => array
            (
                ['Welcome'] => array
                    (
                        ['offer'] => 'Offer repeater field example text'
                        ['bonus'] => 'Bonus repeater field example text'
                    )
            )
        [13] => array
            (
                ['Welcome'] => array
                    (
                        ['offer'] => 'Offer repeater field example text'
                        ['bonus'] => 'Bonus repeater field example text'
                    )
                ['30-OFF'] => array
                    (
                        ['offer'] => 'Offer repeater field example text'
                        ['bonus'] => 'Bonus repeater field example text'
                    )
            )
    )
    

    I'm guessing you want to use this data to perhaps loop offer information for passed car ids.

    Good luck hope this helps 👍🏼