Support

Account

Home Forums Add-ons Repeater Field Query posts with ACF values from a subfield

Solved

Query posts with ACF values from a subfield

    • danilab

    • November 22, 2020 at 9:47 am

    Hi, is there a way to query posts if values of relationship inside a repeater matching with the page id?

    On CPT ‘recipes’ – repeater is listing people recommending the recipe, I’m unable to use the RELATIONSHIP outside REPEATER field as I need to include a comment from this person. So the structure looks like this REPEATER=’people_recommended_quotes’ with RELATIONSHIP=’person’ and TEXT=’comment’

    Would that be the right logic for listing recipes that person recommended alongside related comment?

    Thanks!

    • danilab

    • November 22, 2020 at 11:02 pm

    An update, I’m trying to use an example from here 4. Sub custom field values
    I’ve updated REPEATER name to ‘quote’

    <?php
    
        // filter
        function my_posts_where( $where ) {
    
        	$where = str_replace("meta_key = 'quote_$", "meta_key LIKE 'quote_%", $where);
    
        	return $where;
        }
    
        add_filter('posts_where', 'my_posts_where');
    
        // vars
        $person = '"' . get_the_ID() . '"';
    
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
        // args
        $args = array(
        	'posts_per_page' => 10,
            'paged' => $paged,
            'order' => 'DESC',
        	'post_type' => 'recipes',
        	'meta_query' => array(
            	array(
            		'key' => 'quote_$_person',
            		'value' => '"' . get_the_ID() . '"',
            		'compare' => 'LIKE',
            	)
            )
        );
    
        $the_query = new WP_Query( $args );
        if($the_query->have_posts() ) :
            while ( $the_query->have_posts() ) :
               $the_query->the_post();
    
               get_template_part( 'template-parts/content-recipe-card', get_post_format() );
    
            endwhile;
    
        else: ;
        endif;
    
        ?>
    
    • danilab

    • November 23, 2020 at 12:15 am

    Update this was sorted with 4. Sub custom field values

    I’ve changed the REPEATER name to ‘quote’

    
    // filter
        function my_posts_where( $where ) {
    
        	$where = str_replace("meta_key = 'quote_$", "meta_key LIKE 'quote_%", $where);
    
        	return $where;
        }
    
        add_filter('posts_where', 'my_posts_where');
    
        // vars
        $person = '"' . get_the_ID() . '"';
        $personID = get_the_ID();
    
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
        // args
        $args = array(
        	'posts_per_page' => 10,
            'paged' => $paged,
            'order' => 'DESC',
        	'post_type'		=> 'recipes',
        	'meta_query' => array(
            	array(
            		'key' => 'quote_$_person',
            		'value' => '"' . get_the_ID() . '"',
            		'compare' => 'LIKE',
            	)
            )
        );
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.