Support

Account

Home Forums Add-ons Repeater Field Repeater Relationship Post Fields from Array

Solved

Repeater Relationship Post Fields from Array

  • I think I bit off a little more than I could chew this evening. Please let me know if you see a solution here.

    I have some posts with meta_values of ‘internal’ or ‘external’. I have some pages where I want to list those posts’ titles and case_links as relevant, along with some custom descriptions per page.

    Page A – Internal Posts
    Post X (link)
    Custom ‘page a’ description here…

    Post Y (link)
    Custom ‘page a’ description here…

    Page A – External Posts
    Post Z (link)
    Custom ‘page a’ description here…

    I have a functional array that can break up my posts by internal/external.

    <?php
    $args = array(
    'numberposts'   => '-1',
    'orderby'       => 'title',
    'order'         => 'ASC',
    'post_type'     => 'case_studies',
    'meta_key'      => 'case_study_type',
    'meta_value'    => 'Internal'
    );
    $internal_cs_query = new WP_Query( $args );
    ?>
    <?php if( $internal_cs_query->have_posts() ): ?>
    <?php while ( $internal_cs_query->have_posts() ) : $internal_cs_query->the_post(); ?>
    <!-- ??? -->
    <?php endwhile; ?>  
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    I also have a functional repeater query that gives me the title and the custom description of related posts with custom descriptions, but not much more.

    <?php if ( get_field('test_cases') ): ?>
    <?php while(has_sub_field('test_cases')): ?>
    <?php $cases = get_sub_field('case_relationship'); ?>                                    
    <?php if ( $cases ) : ?>
    <?php foreach ( $cases as $case) : ?>
    <p><?php echo get_the_title( $case ); ?> </p>
    <p><?php the_sub_field('case_custom_desc'); ?></p>
    <p><a href="<?php  ?>">(link)</a></p>
    <?php endforeach; ?>
    <?php endif; ?>
    <?php endwhile; ?>
    <?php endif; ?>
    

    What I can’t figure out how to do is 1) combine the queries to separate those lists and 2) Pull in that case_link text from the post (it’s not a permalink, just text).

  • Hi @rktlwm

    As for creating the 2 lists. I would just duplicate your code and change 'meta_value' => 'Internal' to 'meta_value' => 'External'.

    You can then have full control over the headings above both these querys / loops.

    As for your repeater field question, I’m not sure I understand what you are trying to do. Perhaps all you need to do is place this code INSIDE the cusotm loop you have made?

    Your initial description shows the post title, link and description. I don’t understand how a repeater field is related to this. Perhaps you could use screenshots to help describe the data layout?

    Thanks
    E

  • Thanks, @elliot. I’ll definitely duplicate this for the External cases query.

    I’m trying to combine these queries but not sure how. The repeater provides a way for us to select multiple cases’ values (title and custom field (link)) for the page and add custom descriptions to each of those.

    If it wasn’t for the custom descriptions, I would just use a relationship field, yes.

    1. Get cases’ titles and custom_field_link
    2. Put a custom description to each case
    3. Divide these listings up in the layout as internal and external

    Sorry if I’m making it more confusing than it needs to be. Love these plugins.

  • Hi @rktlwm

    Thanks for the follow up and the attachments. I can now see how you need to work with the data.

    What you need to do is loop over the repeater field and look at the selected case. Load the case’s type (internal / external) and then append this data to an array.

    After the repeater field loop is complete, you should have an array containing all the external and all the internal cases. You can then loop through these and output the 2 lists like so:

    <?php 
    
    $internal = array();
    $external = array();
    
    if ( get_field('test_cases') ): ?>
    	<?php while(has_sub_field('test_cases')): 
    		
    		// get case
    		$case = get_sub_field('case_relationship');
    		$case = array_pop($case);
    		
    		
    		// get case data
    		$data = array(
    			'type'			=> get_field('type', $case),
    			'description'	=> get_sub_field('case_custom_desc'),
    			'title'			=> get_the_title($case),
    			'link'			=> get_permalink($case)
    		);
    		
    		
    		
    		$type = get_field('type', $case);
    		$description = get_sub_field('case_custom_desc');
    		$title = get_the_title($case);
    		$link = get_permalink($case);
    		
    		
    		if( $type == 'internal' )
    		{
    			$internal[] = $data;
    		}
    		else
    		{
    			$external[] = $data;
    		}
    		
    		?>
    	<?php endwhile; ?>
    <?php endif; ?>

    Please note the above code is untested. So with this code, you will have all the case data in 2 arrays (internal and external) which you can loop over and output into the 2 columns for each post.

    Good luck

    Thanks
    E

  • Thanks, man! I’m going to go plug that into some more templates.

    Array pop!

  • [Edited to remove sleepy mistakes…]

    Elliot, thanks again for the info. For anyone who is interested, I wanted to post some code here that worked for me in using a post_object within a repeater. I had originally started this way but couldn’t find many examples on post_object syntax to copy… That’s why I went with the relationship in a repeater.

    Your example for a relationship within a repeater worked for sure, but the interface was actually a little bit difficult for my editor to figure out because it allows multiple entries within one repeater row. The single-select post_object seems better to me because we want just 1. related case A, custom description A … 2. related case B, custom description B.

    Here is my first pass at adapting code from this other question about post objects in repeaters. I’m probably doing a number of things wrong here (I run it twice, there are probably unused variables), but I get the result I’m looking for, at 3am no less! If anyone cares to optimize or correct me, please feel free!

    THANKS

    
    <!-- CASE STUDIES 
    ================================== -->
    <?php 
    if ( get_field('strategy_case_studies') ): ?>
    <section class="valueCaseStudies">
        <div class="container">
            <div class="row">
                <div class="col-md-8">
                    <h3>INTERNAL Case Studies</h3>
                    <div class="row">
                
                <?php while( has_sub_field('strategy_case_studies')) 
                { ?>
    
                <?php 
                    $the_case->post_title; //post title of related case selected in repeater
                    $case_type = get_field('case_study_type', $the_case); // internal or external
                    $the_case = get_sub_field('s_cs_select'); // the repeater
                    $case_desc = get_sub_field('s_cs_description'); // the custom description
                ?>
                    <?php if ( $case_type == 'Internal' ) { ?>
                            <div class="col-md-6">
                                <p><strong><?php echo get_the_title($the_case->ID);  ?></strong>
                                <br /><?php echo $case_desc; ?> 
                                <a href="<?php echo get_permalink($the_case->ID); ?>">(link)</a>
                                </p>
                            </div>                
                    <?php } else {} ?>
                    <?php } ?>                
                    </div>
                </div>
                <?php endif; ?>
                <?php if ( get_field('strategy_case_studies') ): ?>
                <div class="col-md-4">
                    <h3>EXTERNAL Case Studies</h3>
                <?php while( has_sub_field('strategy_case_studies')) 
                { ?>
                <?php 
                    $the_case->post_title; //post title
                    $the_case = get_sub_field('s_cs_select');
                    $case_type = get_field('case_study_type', $the_case);
                    $case_desc = get_sub_field('s_cs_description');
                ?>
                <?php if ( $case_type == 'External' ) { 
    // trying to display internal and external types separately was hard to figure out ... this is probably not the best way 
    ?>            
                    <p><strong><?php echo get_the_title($the_case->ID);  ?></strong>
                    <br /><?php echo $case_desc; ?> 
                    <a href="<?php echo get_permalink($the_case->ID); ?>">(link)</a>
                    </p>
                <?php } else {} ?>
                <?php } ?>                
                </div>    
            </div>
        </div>
    </section>
    <?php endif; ?>
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Repeater Relationship Post Fields from Array’ is closed to new replies.