Support

Account

Home Forums Add-ons Repeater Field query and return rows from repeater

Solved

query and return rows from repeater

  • I’m trying to make an array out of a row from subfiels from all my custom posts.

    I have a CPT named “al_product”. Each product has a repeater entitled “product_variations”. One of the sub_fields in that repeater is called “part_number”.

    I’d like to pull part_numbers from each row in the product_variations field from each product.

    The reason I’m doing this is to make each of the part numbers a row in a drop down select field. Here is what I’ve come up with so far…

    function populate_cpt_titles( $form ) {
    	foreach ( $form['fields'] as &$field ) {
    		if ( $field->type != 'multiselect' || strpos( $field->cssClass, 'populate_al_products' ) === false ) {
    	    		continue;
    		}
    
    		$field->placeholder = 'Select an your product';
    
    		$args = [
    			'posts_per_page'   => -1,
    			'order'            => 'ASC',
    			'orderby'          => 'post_title',
    			'post_type'        => 'al_product', 
    			'post_status'      => 'publish',
    		];
    		$custom_posts = get_posts( $args );
    
    		$options = [];
    		foreach( $custom_posts as $custom_post ) {
    			$options[] = ['text' => $custom_post->post_title, 'value' => $custom_post->post_title];
    			if ( ($custom_post->product_variations) ) $options[] = ['text' => $custom_post->product_variations, 'value' => 'yes-array'];
    		}
    
    		$field->choices = $options;
    	}
    
    	return $form;
    }

    in my foreach loop the first line works and title is pulled into my select field.
    the 2nd line (if $custom_post) works as is, but I’m not sure how the data is set in there. As is above it just returns how many variation rows there are for each product.
    I’ve tested if (is_array($custom_post->product_variations) and that returns nothing?

    What am I missing?

  • This

    
    $custom_post->product_variations
    

    will return nothing. The repeater field in not a property of a WP_Post object.

    To get an array for the repeater you would need to do

    
    $value = get_field('product_variations', $custom_post->ID);
    

    So if I understand what you want to do (which I’m not sure I do) then the line should be

    
    if ((get_field('product_variations', $custom_post->ID)) $options[] = ['text' => get_field('product_variations', $custom_post->ID), 'value' => 'yes-array'];
    
  • I’m in fact trying to get the actual variations.
    so if i have a product set like this:

    Product A
    variation – 001
    variatin – 002
    variation – 003

    Product B
    variation – 123
    variation – 124
    variation – 125

    I want to have an array that shows

    product a, 001, 002, 003, product b, 123, 124, 125

  • I don’t know how to fit this into the code you’re supplied, but you need to loop over the repeater as explained in the repeater documentation and get the value for each row. There isn’t any way to get the values of one sub field in an array.

  • Yeah, I guess I could have been more clear on that. I got it to work this way:

    function populate_cpt_titles( $form ) {
    	foreach ( $form['fields'] as $field ) {
    		if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_al_products' ) === false ) {
    	    		continue;
    		}
    
    		$field->placeholder = 'Select an your product';
    
    		$args = [
    			'posts_per_page'   => -1,
    			'order'            => 'ASC',
    			'orderby'          => 'post_title',
    			'post_type'        => 'al_product', 
    			'post_status'      => 'publish',
    		];
    		$custom_posts = get_posts( $args );
    
    		$options = [];
    		foreach( $custom_posts as $custom_post ) {
    			$options[] = ['text' => $custom_post->post_title, 'value' => $custom_post->post_title];
    
                $subfields = get_field('product_variations', $custom_post->ID);
    			if ($subfields) {
                    foreach ($subfields as $subfield) {
                        $options[] = ['text' => $subfield['part_number'], 'value' => $subfield['part_number'] ];
                    }
                }
    		}
    
    		$field->choices = $options;
    	}
    	return $form;
    }
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.