• Resolved markburton52

    (@markburton52)


    I’m trying to get WP user data to populate custom field types.
    I have the aditional field ids in extended user profiles (for a custom user role: artist) and can access them in a pod and pod template so they appear in a page using that template. I’ve tried to create a relational field type so there would be a drop down of an administrator to select the relevant field value from the multiple user profiles. E.g. I have a field show name, and each user (artist in this case) has a specific value – I want to pull that value into the pod-defined custom post type “artist”. I can’t get it to work. I get the user display name, not the value of the field I’ve specified displaying. If I enable “more fields” in the artist page, then I have drop-downs available – they are the user display names and I want to pull the user profile field data in.
    What am I doing wrong?

Viewing 9 replies - 16 through 24 (of 24 total)
  • Plugin Support Paul Clark

    (@pdclark)

    Please share the exact error you’re getting from PHP error logs.

    Plugin Support Paul Clark

    (@pdclark)

    The code you’ve shared works for me in testing.

    So the exact error message is necessary.

    Perhaps you’ve done something like excluded an opening <?php tag or included a duplicate <?php tag.

    Thread Starter markburton52

    (@markburton52)

    Hi Paul, thanks, yes, I’m not sure what was wrong as it works now. I just need to figure out how to make the user field content available for field types that are other than simple text strings. I’ll look at support forums and possibly come back in another tread if I’m still stuck.

    Plugin Support Paul Clark

    (@pdclark)

    Great. See https://docs.pods.io/code/pods/ for examples of using the pods() function to display data. For example, $label = pods( 'user', $id )->display( 'some_field_name' ); within the foreach where $id is the user ID.

    get_user_meta() should also be sufficient for all data types… for example with a relationship, it would return an array of of IDs if Pods Admin > Settings > Watch WP metadata calls is disabled, and an array of arrays if it is enabled.

    You can inspect the contents of any variable, for example $data by outputting it to the PHP error log with error_log( print_r( $data, true ) ); , where the second argument being set to true causes print_r() to return the value to pass to error_log() instead of outputting inline.

    Thread Starter markburton52

    (@markburton52)

    Thanks, I tried using the display function but nothing changed.

    My problem now is that while the filter will display the first relationship field, the two subsequent ones I’ve added behave weirdly.

    the first, which is a text area field displays the user display name, although in the custom post editor I can pick the field value from the array of possible values.

    the second one, which is a checkbox-populated field doesn’t even give me the drop down options.

    I’ve tried both the template method and the block method to display the code, as well as the display command in the php (take n out again in the code below).

    Could it be that including the three filters in the same snippet in this way only works for the first one?

    Here is the php snippet as it stands now:

    add_filter(
    	'pods_field_pick_data',
    	function( $data, $name, $value, $options, $pod, $id ){
    
    		$relationship_field_name = 'dbem_caf_bring_show_name';
    		$post_type = 'artist';
    
    		if ( false !== strpos( $name, $relationship_field_name ) ) {
    			foreach( $data as $id => & $label ) {
    				$label = get_user_meta( $id, 'dbem_caf_show_name', true );
    			}
    		}
    		return $data;
    	},
    	10,
    	6
    );
    add_filter(
    	'pods_field_pick_data',
    	function( $data, $name, $value, $options, $pod, $id ){
    
    		$relationship_field_name = 'dbem_caf_bring_about_the_artist';
    		$post_type = 'artist';
    
    		if ( false !== strpos( $name, $relationship_field_name ) ) {
    			foreach( $data as $id => & $label ) {
    				$label = get_user_meta( $id, 'dbem_caf_about_the_artist', true );
    			}
    		}
    		return $data;
    	},
    	10,
    	6
    );
    add_filter(
    	'pods_field_pick_data',
    	function( $data, $name, $value, $options, $pod, $id ){
    
    		$relationship_field_name = 'dbem_caf_bring_discipline';
    		$post_type = 'artist';
    
    		if ( false !== strpos( $name, $relationship_field_name ) ) {
    			foreach( $data as $id => & $label ) {
    				$label = get_user_meta( $id, 'dbem_caf_discipline', true );
    			}
    		}
    		return $data;
    	},
    	10,
    	6
    );
    Plugin Support Paul Clark

    (@pdclark)

    the first, which is a text area field displays the user display name, although in the custom post editor I can pick the field value from the array of possible values.

    I’m not sure if you’re using a Plain Text text area field of a WYSIWYG text area field, but I tested with both and the below code works with both.

    I’m not sure what you mean by “it displays the user name” —?do you mean when you try to display the field in a template? Keep in mind that these filters do not change the stored value, which is a relationship to a User ID. To display the fields in a template, one would still need to get the field from the associated user. So all your relationship fields with bring in the name are storing a User ID. To get the field on the user, one would get the User ID, then get the field. This can be done with dot notation as shown in the screenshotted shortcode template below, or manually with PHP.

    Here are the fields on the Add Artist page:

    And the fields on the User Edit screen:

    An example shortcode displaying the related fields using magic tags. Note the dot-notation for traversal, and that I used a relationship to a Taxonomy for discipline, as I was having trouble getting Simple Relationship to display more than one value with {@_value} within an [each] instead of {@name} @sc0ttkclark. It would be {@post_title} instead of {@_value} if the relationship is to a post type.

    And the display results of that template:

    Here is the code for your three fields formatted into a single function. Not because there is any issue with having three functions, but because you seemed to have that preference:

    <?php
    add_filter(
    	'pods_field_pick_data',
    	function( $data, $name, $value, $options, $pod, $id ){
    
    		$post_type = 'artist';
    
    		$relationship_field_name = 'dbem_caf_bring_show_name';
    		if ( false !== strpos( $name, $relationship_field_name ) ) {
    			foreach( $data as $id => & $label ) {
    				$label = get_user_meta( $id, 'dbem_caf_show_name', true );
    			}
    		}
    
    		$relationship_field_name = 'dbem_caf_bring_about_the_artist';
    		if ( false !== strpos( $name, $relationship_field_name ) ) {
    			foreach( $data as $id => & $label ) {
    				$label = get_user_meta( $id, 'dbem_caf_about_the_artist', true );
    			}
    		}
    
    		$relationship_field_name = 'dbem_caf_bring_discipline';
    		if ( false !== strpos( $name, $relationship_field_name ) ) {
    			foreach( $data as $id => & $label ) {
    				$label = pods( 'user', $id )->display( 'dbem_caf_discipline' );
    
    				if ( empty( $label ) ) {
    					$label = 'None selected';
    				}
    			}
    		}
    
    		return $data;
    	},
    	10,
    	6
    );
    Thread Starter markburton52

    (@markburton52)

    Thanks for this Paul. Unfortunately I can’t see the images you’ve included, wither in Firefox or Chromium. I’ve tried disabling settings that could obscure them – any thoughts?

    Thread Starter markburton52

    (@markburton52)

    The snippet works but for some reason I can’t see the information after the lines, a) “relationship is to a post type.” and b) “And the display results of that template:” so while I can now pull the codes in to the post editor, I’m still struggling to display them.

    Thanks again

    Plugin Support Paul Clark

    (@pdclark)

    Here is a PDF of how I see the post with images and comments.

Viewing 9 replies - 16 through 24 (of 24 total)
  • The topic ‘user reference in relationship fields’ is closed to new replies.