Support

Account

Home Forums ACF PRO Reverse query on author.php from User Relationship field

Solved

Reverse query on author.php from User Relationship field

  • I am trying to retrieve fields from the attorney custom post type and place them on the on the author.php page template.

    I grabbed the loop from this article http://www.advancedcustomfields.com/resources/querying-relationship-fields/

    I have a custom post type named attorney where I am selecting a user through the User Relationship field. It properly stores the author ID into the database.

    From the article above, I should be able to compare the author ID that is stored from the custom field with the author ID passed through get_the_author(‘ID’) function on the author.php page template. I think the problem has something to do with my custom field not being populated with the proper variable.

    I can echo the author ID just fine.

    The loop processes just fine when I change LIKE to NOT LIKE so I know it works.

    That leaves me to believe I am not grabbing the custom field value from vbmmp_attorney_related_user_profile

    Here is my function that is placed on the author.php page template.

    Thanks for the help!

    		add_action('genesis_after_header','vbmmp_display_author_name');
    		function vbmmp_display_author_name() {
    		
    			$author_id = get_the_author_meta('ID');
    
    			echo $author_id; // This shows me that the function above is grabbing a single value
    
    			global $post;
    
    			$items = get_posts(array(
    				'post_type' => 'attorney',
    				'posts_per_page' => -1,
    				'meta_query' => array(
    					array(
    						'key' => 'vbmmp_attorney_related_user_profile', // name of custom field
    						'value' => '"' . $author_id . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    						'compare' => 'LIKE'
    					)
    				)
    			));
    
    			echo '<ul>';
    			 	foreach( $items as $item ): 
    					echo '<li>';
    						echo get_the_post_thumbnail($item->ID, 'author-avatar', array( 'class' => 'author-image' ) );
    						echo '<h1 class="archive-title" itemprop="headline">'.get_the_title( $item->ID ).'</h1>'; 
    					echo '</li>';
    				endforeach;
    			echo '</ul>';
    
    		}
  • Hi @claytoncollie

    That’s weird. Your code looks OK for me. Could you please try to add the ID manually and change the compare option? You can try the following codes:

    $items = get_posts(array(
    	'post_type' => 'attorney',
    	'posts_per_page' => -1,
    	'meta_query' => array(
    		array(
    			'key' => 'vbmmp_attorney_related_user_profile', // name of custom field
    			'value' => '"123"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    			'compare' => 'LIKE'
    		)
    	)
    ));
    $items = get_posts(array(
    	'post_type' => 'attorney',
    	'posts_per_page' => -1,
    	'meta_query' => array(
    		array(
    			'key' => 'vbmmp_attorney_related_user_profile', // name of custom field
    			'value' => '123', // matches exaclty "123", not just 123. This prevents a match for "1234"
    			'compare' => '='
    		)
    	)
    ));
    $items = get_posts(array(
    	'post_type' => 'attorney',
    	'posts_per_page' => -1,
    	'meta_key' => 'vbmmp_attorney_related_user_profile',
    	'meta_value' => '123',
    	'meta_compare' => '=',
    ));

    You can also try to get the post based on another custom field to check it up. To learn more about the custom field query, please take a look at this page: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters.

    I hope this helps.

  • None of these worked. I know the author ID was 14 so I put that into the meta_key and meta_value and then did you suggestions but still not luck. Very strange.

  • Hi @claytoncollie

    That’s weird. Did you set “Select multiple values?” to “Yes”? Could you please share the result of the following code?

    var_dump(get_field('vbmmp_attorney_related_user_profile', 99, false));

    Where 99 is the ID of your attorney post.

    Looking forward to your reply 🙂

  • string ’14’ (length=2)

    I wonder if the problem is that the value is formatted with single quotes around it.

    I am returning 14 as the author ID and then on the vardump for that attorney custom post type I am getting ’14’ with quotes. Would that be it?

    I went into the custom field settings and placed allow null and allow multiple values to YES.

  • Hi @claytoncollie

    Thanks for testing it out.

    You don’t need to set the multiple values to “yes”. I just wanted to make sure that you have a single value in your settings.

    The var_dump() function will always show the quote for a string value, so don’t worry about it.

    Do you have a staging site where I can test this issue? If you have, could you please share the admin credentials in a private reply.

    Thanks!

  • This reply has been marked as private.
  • Hi @claytoncollie

    I’m afraid the theme editor is disabled in your wp-config.php. Could you please enable it so I can check the template? This page should give you more idea about it: https://community.1and1.com/enabling-the-wordpress-theme-and-plugin-editor-wordpress-free-mode/.

    Thanks!

  • This reply has been marked as private.
  • Hi @claytoncollie

    I’ve just tested it using the second code structure I gave you before, and it was working as it should. Here is the code:

    'meta_query' => array(
        array(
            'key' => 'vbmmp_attorney_related_user_profile', // name of custom field
            'value' => $author_id,
            'compare' => '='
        )
    )

    Could you please check it out? Thanks!

  • Thanks James. Sorry I couldn’t get it the first time. Really appreciate your help on this one!

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

You must be logged in to reply to this topic.