Support

Account

Home Forums Add-ons Repeater Field Query Posts for Repeater Subfield "User" Equal to Current Author

Solved

Query Posts for Repeater Subfield "User" Equal to Current Author

  • I’m having trouble following the documentation and forum examples to query posts which have repeater subfield “user” IDs which match the current author we’re viewing (author.php).

    I’ve tried a WP_Query loop, get_posts loop, and $wpdb based on what I’ve found with no results.

    I’ve verified no typos. Verified that $author_id returns the current author ID.

    Repeater field is “people_list” and user subfield is “person_list_user”. I know that the first row of the people_list repeater field on page ID 569 specifies author ID 8 which is the author I’m viewing. I think I’ve exhausted all variable and can say for certain my query attempts are faulty.

    WP_Query Attempt:

    
    // args
    $args = array(
    	'post_type' => 'page',
    	'meta_query' => array(
    		array(
    			'key' => '{people_list}_%_{person_list_user}',
    			'value' => '"' . $author_id . '"',
    			'compare' => 'LIKE'
    		)
    	)
    );
    $the_query = new WP_Query( $args );
    if( $the_query->have_posts() ):
    while ( $the_query->have_posts() ) : $the_query->the_post();
    

    $wpdb Attempt:

    
    global $wpdb;
    $projects = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE meta_key LIKE 'people_list_%_person_list_user' AND meta_value = $author_id", OBJECT );
    foreach($projects as $project) {
    	echo $project->ID;
    }
    

    get_posts Attempt:

    
    $projects = get_posts(array(
    	'post_type' => 'page',
    	'meta_query' => array(
    		array(
    			'key'		=> 'person_list_user',
    			'compare'	=> '=',
    			'value'		=> $author_id,
    		),
    	)
    ));
    $project_query = new WP_Query( $projects );
    echo '<pre>'; print_r($projects); echo '</pre>';
    
  • Hi @inhouse

    I’m afraid I don’t understand your current setup, so please forgive me if I’m wrong.

    Could you please check the documentation here (under the Sub custom field values section): https://www.advancedcustomfields.com/resources/query-posts-custom-fields/? Based on that documentation, your code should be like this:

    // filter
    function my_posts_where( $where ) {
    	
    	$where = str_replace("meta_key = 'people_list_%", "meta_key LIKE 'people_list_%", $where);
    
    	return $where;
    }
    
    add_filter('posts_where', 'my_posts_where');
    
    // args
    $args = array(
    	'post_type' => 'page',
    	'meta_query' => array(
    		array(
    			'key' => 'people_list_%_person_list_user',
    			'value' => '"' . $author_id . '"',
    			'compare' => 'LIKE'
    		)
    	)
    );
    $the_query = new WP_Query( $args );
    if( $the_query->have_posts() ):
    while ( $the_query->have_posts() ) : $the_query->the_post();

    I hope this helps 🙂

  • Hi James! Thanks so much. I’ve read through that documentation before and tried the code you provided but the query is empty. Page ID 569 should be returned but it’s not. The current author ID is 8 so that part is correct.

    I tried placing the function in my functions file and also tried moving it to the author template.

    I’m not sure I understand how the meta_query should be working. We should be looking for any page that has a sub field (“person_list_user”) with a value of author ID 8 since we’re viewing this author’s profile. Page ID 569 should be returned. The query seems correct but I’m not sure what’s wrong.

    I’ve tried inputting sub field data to other pages using other users with no luck.

    
    echo '<pre>'; print_r($the_query); echo '</pre>';
    WP_Query Object
    (
        [query] => Array
            (
                [post_type] => page
                [meta_query] => Array
                    (
                        [0] => Array
                            (
                                [key] => people_list_%_person_list_user
                                [value] => "8"
                                [compare] => LIKE
                            )
    
                    )
    
            )
    
        [query_vars] => Array
            (
                [post_type] => page
                [meta_query] => Array
                    (
                        [0] => Array
                            (
                                [key] => people_list_%_person_list_user
                                [value] => "8"
                                [compare] => LIKE
                            )
    
                    )
    
                [error] => 
                [m] => 
                [p] => 0
                [post_parent] => 
                [subpost] => 
                [subpost_id] => 
                [attachment] => 
                [attachment_id] => 0
                [name] => 
                [static] => 
                [pagename] => 
                [page_id] => 0
                [second] => 
                [minute] => 
                [hour] => 
                [day] => 0
                [monthnum] => 0
                [year] => 0
                [w] => 0
                [category_name] => 
                [tag] => 
                [cat] => 
                [tag_id] => 
                [author] => 
                [author_name] => 
                [feed] => 
                [tb] => 
                [paged] => 0
                [meta_key] => 
                [meta_value] => 
                [preview] => 
                [s] => 
                [sentence] => 
                [title] => 
                [fields] => 
                [menu_order] => 
                [embed] => 
                [category__in] => Array
                    (
                    )
    
                [category__not_in] => Array
                    (
                    )
    
                [category__and] => Array
                    (
                    )
    
                [post__in] => Array
                    (
                    )
    
                [post__not_in] => Array
                    (
                    )
    
                [post_name__in] => Array
                    (
                    )
    
                [tag__in] => Array
                    (
                    )
    
                [tag__not_in] => Array
                    (
                    )
    
                [tag__and] => Array
                    (
                    )
    
                [tag_slug__in] => Array
                    (
                    )
    
                [tag_slug__and] => Array
                    (
                    )
    
                [post_parent__in] => Array
                    (
                    )
    
                [post_parent__not_in] => Array
                    (
                    )
    
                [author__in] => Array
                    (
                    )
    
                [author__not_in] => Array
                    (
                    )
    
                [orderby] => menu_order
                [order] => ASC
                [ignore_sticky_posts] => 
                [suppress_filters] => 
                [cache_results] => 1
                [update_post_term_cache] => 1
                [lazy_load_term_meta] => 1
                [update_post_meta_cache] => 1
                [posts_per_page] => 100
                [nopaging] => 
                [comments_per_page] => 50
                [no_found_rows] => 
            )
    
        [tax_query] => WP_Tax_Query Object
            (
                [queries] => Array
                    (
                    )
    
                [relation] => AND
                [table_aliases:protected] => Array
                    (
                    )
    
                [queried_terms] => Array
                    (
                    )
    
                [primary_table] => wp_posts
                [primary_id_column] => ID
            )
    
        [meta_query] => WP_Meta_Query Object
            (
                [queries] => Array
                    (
                        [0] => Array
                            (
                                [key] => people_list_%_person_list_user
                                [value] => "8"
                                [compare] => LIKE
                            )
    
                        [relation] => OR
                    )
    
                [relation] => AND
                [meta_table] => wp_postmeta
                [meta_id_column] => post_id
                [primary_table] => wp_posts
                [primary_id_column] => ID
                [table_aliases:protected] => Array
                    (
                        [0] => wp_postmeta
                    )
    
                [clauses:protected] => Array
                    (
                        [wp_postmeta] => Array
                            (
                                [key] => people_list_%_person_list_user
                                [value] => "8"
                                [compare] => LIKE
                                [alias] => wp_postmeta
                                [cast] => CHAR
                            )
    
                    )
    
                [has_or_relation:protected] => 
            )
    
        [date_query] => 
        [request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1  AND ( 
      ( wp_postmeta.meta_key LIKE 'people_list_%_person_list_user' AND wp_postmeta.meta_value LIKE '%\"8\"%' )
    ) AND wp_posts.post_type = 'page' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order ASC LIMIT 0, 100
        [posts] => Array
            (
            )
    
        [post_count] => 0
        [current_post] => -1
        [in_the_loop] => 
        [comment_count] => 0
        [current_comment] => -1
        [found_posts] => 0
        [max_num_pages] => 0
        [max_num_comment_pages] => 0
        [is_single] => 
        [is_preview] => 
        [is_page] => 
        [is_archive] => 
        [is_date] => 
        [is_year] => 
        [is_month] => 
        [is_day] => 
        [is_time] => 
        [is_author] => 
        [is_category] => 
        [is_tag] => 
        [is_tax] => 
        [is_search] => 
        [is_feed] => 
        [is_comment_feed] => 
        [is_trackback] => 
        [is_home] => 1
        [is_404] => 
        [is_embed] => 
        [is_paged] => 
        [is_admin] => 
        [is_attachment] => 
        [is_singular] => 
        [is_robots] => 
        [is_posts_page] => 
        [is_post_type_archive] => 
        [query_vars_hash:WP_Query:private] => e3e584584a2f66031a5baf3571faac4c
        [query_vars_changed:WP_Query:private] => 1
        [thumbnails_cached] => 
        [stopwords:WP_Query:private] => 
        [compat_fields:WP_Query:private] => Array
            (
                [0] => query_vars_hash
                [1] => query_vars_changed
            )
    
        [compat_methods:WP_Query:private] => Array
            (
                [0] => init_query_flags
                [1] => parse_tax_query
            )
    
    )
    
  • Hi @inhouse

    The SQL query looks correct for me. Could you please share the JSON export file of your field group so I can test it out on my installation?

    Also, could you please debug the repeater data for me like the following?

    echo '<pre>';
    var_dump( get_field('people_list', 569) );
    echo '</pre>';

    Thanks 🙂

  • Hi James,

    I uploaded the JSON file to DropBox here.

    I’m only interested in row 1 from the repeater list which contains the User ACF field targeting author ID 8 which is the author profile we’re viewing.

    Debug:

    
    array(2) {
      [0]=>
      array(8) {
        ["people_list_person_login"]=>
        bool(true)
        ["person_list_user"]=>
        array(11) {
          ["ID"]=>
          int(8)
          ["user_firstname"]=>
          string(8) "John"
          ["user_lastname"]=>
          string(9) "Smith"
          ["nickname"]=>
          string(18) "John Smith"
          ["user_nicename"]=>
          string(15) "johnsmith"
          ["display_name"]=>
          string(18) "John Smith"
          ["user_email"]=>
          string(21) "[email protected]"
          ["user_url"]=>
          string(0) ""
          ["user_registered"]=>
          string(19) "2016-11-09 15:25:48"
          ["user_description"]=>
          string(0) ""
          ["user_avatar"]=>
          bool(false)
        }
        ["person_list_name"]=>
        string(0) ""
        ["person_list_title"]=>
        string(0) ""
        ["person_list_affiliation"]=>
        string(0) ""
        ["person_list_location"]=>
        string(0) ""
        ["person_list_url"]=>
        string(0) ""
        ["person_list_photo"]=>
        bool(false)
      }
      [1]=>
      array(8) {
        ["people_list_person_login"]=>
        bool(false)
        ["person_list_user"]=>
        NULL
        ["person_list_name"]=>
        string(17) "Jane Smith"
        ["person_list_title"]=>
        string(0) ""
        ["person_list_affiliation"]=>
        string(0) ""
        ["person_list_location"]=>
        string(0) ""
        ["person_list_url"]=>
        string(47) "http://domain.com/staff/jane-smith/"
        ["person_list_photo"]=>
        array(18) {
          ["ID"]=>
          int(2357)
          ["id"]=>
          int(2357)
          ["title"]=>
          string(16) "JaneSmith"
          ["filename"]=>
          string(21) "JaneSmith.jpeg"
          ["url"]=>
          string(74) "http://domain.com/wp-content/uploads/2014/05/JaneSmith.jpeg"
          ["alt"]=>
          string(0) ""
          ["author"]=>
          string(1) "1"
          ["description"]=>
          string(0) ""
          ["caption"]=>
          string(0) ""
          ["name"]=>
          string(16) "janesmith"
          ["date"]=>
          string(19) "2014-05-01 00:00:00"
          ["modified"]=>
          string(19) "2016-11-14 18:37:32"
          ["mime_type"]=>
          string(10) "image/jpeg"
          ["type"]=>
          string(5) "image"
          ["icon"]=>
          string(62) "http://domain.com/wp-includes/images/media/default.png"
          ["width"]=>
          int(968)
          ["height"]=>
          int(974)
          ["sizes"]=>
          array(30) {
            ["thumbnail"]=>
            string(82) "http://domain.com/wp-content/uploads/2014/05/JaneSmith-200x200.jpeg"
            ["thumbnail-width"]=>
            int(200)
            ["thumbnail-height"]=>
            int(200)
            ["medium"]=>
            string(82) "http://domain.com/wp-content/uploads/2014/05/JaneSmith-298x300.jpeg"
            ["medium-width"]=>
            int(298)
            ["medium-height"]=>
            int(300)
            ["medium_large"]=>
            string(82) "http://domain.com/wp-content/uploads/2014/05/JaneSmith-768x773.jpeg"
            ["medium_large-width"]=>
            int(768)
            ["medium_large-height"]=>
            int(773)
            ["large"]=>
            string(74) "http://domain.com/wp-content/uploads/2014/05/JaneSmith.jpeg"
            ["large-width"]=>
            int(968)
            ["large-height"]=>
            int(974)
            ["post-feat-img"]=>
            string(82) "http://domain.com/wp-content/uploads/2014/05/JaneSmith-646x650.jpeg"
            ["post-feat-img-width"]=>
            int(646)
            ["post-feat-img-height"]=>
            int(650)
            ["page-banner"]=>
            string(82) "http://domain.com/wp-content/uploads/2014/05/JaneSmith-968x130.jpeg"
            ["page-banner-width"]=>
            int(968)
            ["page-banner-height"]=>
            int(130)
            ["slideshow"]=>
            string(82) "http://domain.com/wp-content/uploads/2014/05/JaneSmith-968x550.jpeg"
            ["slideshow-width"]=>
            int(968)
            ["slideshow-height"]=>
            int(550)
            ["internal-slideshow"]=>
            string(82) "http://domain.com/wp-content/uploads/2014/05/JaneSmith-830x575.jpeg"
            ["internal-slideshow-width"]=>
            int(830)
            ["internal-slideshow-height"]=>
            int(575)
            ["home-pod"]=>
            string(82) "http://domain.com/wp-content/uploads/2014/05/JaneSmith-320x195.jpeg"
            ["home-pod-width"]=>
            int(320)
            ["home-pod-height"]=>
            int(195)
            ["annual-report-thumb"]=>
            string(82) "http://domain.com/wp-content/uploads/2014/05/JaneSmith-150x188.jpeg"
            ["annual-report-thumb-width"]=>
            int(150)
            ["annual-report-thumb-height"]=>
            int(188)
          }
        }
      }
    }
    
  • Hi @inhouse

    I’ve just checked your setup, and I believe it was because you have set the “Select multiple values?” option for the “person_list_user” field to “No.” In this case, you need to query it like this:

    // filter
    function my_posts_where( $where ) {
    	
    	$where = str_replace("meta_key = 'people_list_%", "meta_key LIKE 'people_list_%", $where);
    
    	return $where;
    }
    
    add_filter('posts_where', 'my_posts_where');
    
    // args
    $args = array(
    	'post_type' => 'page',
    	'meta_query' => array(
    		array(
    			'key' => 'people_list_%_person_list_user',
    			'value' => $author_id,
    			'compare' => '='
    		)
    	)
    );
    $the_query = new WP_Query( $args );
    if( $the_query->have_posts() ):
    while ( $the_query->have_posts() ) : $the_query->the_post();

    Please notice the “value” and “compare” options.

    I hope this helps 🙂

  • @acf-support Thanks so much for the help! I wasn’t aware that distinction made a difference in the loop. I hope this helps someone else.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Query Posts for Repeater Subfield "User" Equal to Current Author’ is closed to new replies.