Support

Account

Home Forums General Issues show array of relationship field values in USER query

Solved

show array of relationship field values in USER query

  • i try to show the relationship values from a custom field that is displayed on user pages, but

    1: when the user has chosen some relationships i get only the ID of the field and
    2: when the user has not chosen any field i get a error (“Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs …”)

    this is my code:

    <?php
    	$values = $user->user_faecher;
    	if (isset($values))	{
    		
    		foreach($values as $value) {
    		    if($value != end($values)){
    		        echo $value;
    		    } else{
    		        echo $value;
    		    }
    		}
    
    	}
    ?>
  • Hi @herrfischer

    I don’t understand your code..

    What is $user->user_faecher? That does not exist in WordPress user object per default.

  • “user_faecher” is the name of the ACF relationship field.

  • With this code the error is gone:

    <?php
    	$values = $user->user_faecher;
    	if (!empty($values))	{
    		
    		foreach($values as $value) {
    		    if($value != end($values)){
    		        echo $value;
    		    } else {
    		        echo $value;
    		    }
    		}
    
    	}
    
    ?>

    but i have still only the IDs … i need the names 🙁 .

  • Yes but I need to know what in the world user_faecher is?
    That value should not exist so $values should just be nothing for you..

    Please post your entire code either here or in a pastebin so I can have a look.

  • hello,
    here is a pastebin of the page template: http://pastebin.com/20BeMiVc
    this page has a user_query and “user_faecher” is a ACF relationship field that lists all wordpress pages on the user pages. the user choses some pages and the title of the pages should appear in the query on each user listing.

    field option:
    field option

    user page:
    user page

    frontend – the result are the IDs of the chosen fields, but i need the names (in this case “Biologie” + “Physik”):
    frontend

  • hello jonathan,
    i found out i have to echo the title, it’s that easy *gg

    <?php
    	$values = $user->user_faecher;
    	if (!empty($values)) {
    		
    		foreach($values as $value) {
    		    if($value != end($values)){
    		        echo get_the_title( $value );
    		    } else {
    		        echo get_the_title( $value );
    		    }
    		}
    
    	}
    ?>

    thanks anyway for your effort!

  • Hi,

    Hm okay.. but if $user is a regular wp user object in WP then $values should not work. To retrieve a user meta field in ACF you need to do
    $values = get_field('user_faecher', 'user_'.$user->ID);

    Also in your code, this part does absolutely nothing:

    
      $args = array (
                                            'meta_query'     => array(
                                                    array(
                                                           
                                                    ),
                                            ),
                                    );
    

    You’re basically passing no arguments into wp_user_query. Which is fine if you’re good with the default arguments but if so you should remove that code along with the $args inside WP_User_Query.

  • thanks jonathan, i need the args for later … . something like:

    $args = array (
    	'order'        => 'ASC',
    	'orderby'      =>  $sortit,
    	'meta_key'     => 'hide_user',
    	'meta_value'   => 'visible'
    );
  • Alright 🙂

    You know your code best of course. Best of luck in your project!

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

The topic ‘show array of relationship field values in USER query’ is closed to new replies.