Support

Account

Home Forums General Issues Querying multiple subfields of the same particular repeater row

Solving

Querying multiple subfields of the same particular repeater row

  • I recently started a topic on here which helped me take a single department page (department content type) and list out the faculty (people content type) that are linked to that department via Post Object.

    Here’s that topic: https://support.advancedcustomfields.com/forums/topic/querying-a-post-object-relation-field-inside-a-repeater/

    I may have pre-maturely marked it solved.

    Say the department is Math. I set up the code to display the faculty appropriately under two different headings: Department Head and Supporting Members. The distinction is made via a checkbox sub-field, right next to the one in which that person is linked to the department (again, by Post Object)

    The problem is the code in the link (and below) is looking at the repeater and subfield and processing it as “if this person is marked as department head in ANY department, list them as the department head in this department.” or “if this person is marked as a supporting member in ANY department, list them under supporting faculty for this department.”

    What I need is for the second part of the meta_query to only apply to the particular row resulting from the first part.

    It looks like this tutorial does a good job of querying specific data to particular repeater rows…is this what I need? And if so, how can I assign it to a different content type?
    OR
    Is there a way to use the code below but tell it that two subfields of the meta_query must be aligned to the same particular row?

    
    <?php
    
    // filter
    function my_posts_where( $where ) {
    	$where = str_replace("meta_key = 'person_department_%", "meta_key LIKE 'person_department_%", $where);
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    
    	// checks if there's a department head
    	// if so, displays that person
    
    	// args
    	$args_faculty_head = array(
    		'posts_per_page'	=> -1,
    		'post_type'		=> 'people',
    		'orderby' => 'title',
    		'order' => 'ASC',
    		'meta_query'	=> array(
    			'relation'		=> 'AND',
    			array(
    				'key'		=> 'person_department_%_person_department_select', // this should be the first sub-field
    				'value' => get_the_ID(),
    				'compare' => '='
    			),
    			array(
    				'key'		=> 'person_department_%_person_department_head', // this should be the second sub-field
    				'value' => '"Yes"',
    				'compare' => 'LIKE'
    			)
    		)
    	);
    
    	// query
    	$the_query = new WP_Query( $args_faculty_head );
    	?>
    
    	<?php if( $the_query->have_posts() ): ?>
    
    		<ul>
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    			<li>
    				<?php the_title(); ?>
    			</li>
    		<?php endwhile; ?>
    		</ul>
    	<?php endif; ?>
    	<?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
    
    • James

    • June 20, 2016 at 4:20 pm

    Hi @charlesr

    I’m afraid I don’t understand the situation you have. Could you please share some screenshots of the issue and add some note on how you want it to be?

    Thanks πŸ™‚

  • Sure, let me try to illustrate this.

    Here are the subfields for the person_department in the the People custom post type.

    Here’s Jeremy’s information in this repeater. He’s the head of the Glass Casting department, but only a “supporting” member in other departments.

    Then, here’s the Glass Casting department “post” in the Department custom post type. It uses single-department.php.

    Jeremy appears as the Head of the Glass Casting department. But he also appears in Supporting faculty. And it seems because the code recognizes that yes he IS a supporting member of *a* department. But I want the query to only search for head or supporting members of this particular department. That way, only the applicable people will appear in each faculty section, un like whats’s showing now.

  • To further clarify, this is how it should be on the single Department page…

    Department Head(s)
    [listing of name(s) of department heads for only this particular department]

    Supporting Faculty
    [listing of names of supporting faculty for only this particular department]

    In the above Glass Casting department page, Jeremy should ONLY appear as the department head, not as a supporting faculty member as well.

    • James

    • June 21, 2016 at 11:32 am

    Hi @charlesr

    I believe you can get the head people by using this query:

    'meta_query'	=> array(
        'relation'		=> 'AND',
        array(
            'key'		=> 'person_department_%_person_department_select', // this should be the first sub-field
            'value' => get_the_ID(),
            'compare' => '='
        ),
        array(
            'key'		=> 'person_department_%_person_faculty_type', // this should be the second sub-field
            'value' => 'head',
            'compare' => '='
        )
    )

    And get the supporting people by using this query:

    'meta_query'	=> array(
        'relation'		=> 'AND',
        array(
            'key'		=> 'person_department_%_person_department_select', // this should be the first sub-field
            'value' => get_the_ID(),
            'compare' => '='
        ),
        array(
            'key'		=> 'person_department_%_person_faculty_type', // this should be the second sub-field
            'value' => 'head',
            'compare' => '!='
        )
    )

    If that doesn’t work, could you please open a new ticket and provide temporary credentials to your site here: https://support.advancedcustomfields.com/new-ticket? Don’t forget to provide the link to this thread in the ticket.

    Thanks πŸ™‚

  • Not able to check now, but I’m pretty sure I tried that solution. The issue is that code, with using the % sign as a variable, checks for that role in ANY department, not the specific one in question.

    But I’ll confirm in a little bit and submit a ticket if I need to. Thanks!

  • Ticket submitted! Thanks.

  • Hi @charlesr @acf-support

    It seems that I have the same issue for one of my website.
    I have a repeater field : resultats
    I have three subfield : rank, country (a relation field), results.

    I want to query post when rank = 3 and country = country-single

    This is my code for querying post :

    $query_args = array(
        'post_type' => 'epreuves',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'       => 'resultats_%_country',
                'compare'   => 'LIKE',
                'value' =>  $global_ID
            ),
            array(
                'key'       => 'resultats_%_rank',
                'compare'   => 'LIKE',
                'value'     => 3,
            )  
        )
    );
    $query = new WP_Query( $query_args );

    It work fine, but if I have my country (france for exemple) in the rank 2 and an other country (USA) in the rank 3 he show me the post where france is in the rank 2.
    It looks like the meta_query don’t search in the row of the repeater but in all the repeater.

    I hope I am clear enough
    Thanks πŸ™‚

  • Hello @arousseau
    I have the same problem! Does anyone know how to solve this problem? Regarding to your example @arousseau , I want to make that meta_query will be search in only one row, but not in all.

    Any ideas?

Viewing 9 posts - 1 through 9 (of 9 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.