• Resolved homonk

    (@homonk)


    Hi

    I am trying to create a shortcode for this page which show an authors user photo and name only if a custom field checkbox on the user profile page is selected. At the moment it is show all the authors – regardless of whether the box is checked or not.

    Here is my shortcode code:

    //Add steering member list shortcode
    function steering_list_authors() { if (get_the_author_meta( "steering", $user->ID)) {
    
    	$authors = get_users(array(
    			'orderby' => 'display_name',
    			'count_totals' => false,
    			'who' => 'authors' ));
    	$list = '';
    	if($authors) :
    		$list .= '<div id="mason2">';
    			foreach($authors as $author) :
    				$list .= '<div class="author-entry">';
    					$archive_url = get_author_posts_url($author->ID);
    					$list .= '<a href="'. $archive_url . '" title="' . __('View ') . $author->display_name . '">';
    					$list .= get_avatar($author->user_email);
    					$list .= '<a>';
    					$list .= '<a href="'. $archive_url . '" title="' . __('View ') . $author->display_name . '"><h3>' . $author->display_name . '</h3></a>';
    				$list .= '</div>';
    			endforeach;
    		$list .= '</div>';
    	endif;
    	return $list;
    }}
    add_shortcode('steering_list', 'steering_list_authors');

    I can’t work out what I need to change to make it dependent on the checkbox. I would be very grateful for any help.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I take it your user field checkbox state is stored in usermeta table?

    You could then add meta_key and meta_value arguments to the array passed to get_users() function to limit results to authors that have a particular value for a certain key.

    Thread Starter homonk

    (@homonk)

    Thanks for replying, I’m still really struggling – could I be doing something else wrong

    I’m using this as the input

    <input type="checkbox" name="steering" id="steering" value="true" <?php if (esc_attr( get_the_author_meta( "steering", $user->ID )) == "true") echo "checked"; ?> />

    and this to save the the values

    update_usermeta( $user_id, 'steering', $_POST['steering'] );

    It is saving in the database on a table wp_usermeta with the meta_key ‘steering’ and the meta_value “true’

    I can’t workout how to use this data

    I tried just adding

    $authors = get_users(array(
    			'orderby' => 'display_name',
    			'count_totals' => false,
    			'who' => 'authors',
    			'steering' => true
    			 ));

    but it didn’t work.

    Any ideas?

    Moderator bcworkz

    (@bcworkz)

    Sorry, I should have posted an example, I tend to overestimate a questioner’s skill level. Do something like this (untested):

    $authors = get_users(array(
    	'meta_key' => 'steering',
    	'meta_value' => true,
    	'orderby' => 'display_name',
    	'count_totals' => false,
    	'who' => 'authors',
    	));

    Thread Starter homonk

    (@homonk)

    Thank you!

    I managed to get it to work combining your info and some code I found elsewhere.

    //Add steering member list shortcode
    function steering_list_authors() { 
    
    $args  = array(
    'orderby' => 'display_name',
    'meta_query' => array(
        array(
            'key' => 'steering',
            'value' => 'true' )
    ));
    
    $wp_user_query = new WP_User_Query($args);
    $authors = $wp_user_query->get_results();
    if (!empty($authors))
    {
        echo '<div id="mason2">';
        foreach ($authors as $author)
        {
    	$archive_url = get_author_posts_url($author->ID);
            $author_info = get_userdata($author->ID);
    	echo '<div class="author-entry">';
    	echo '<a href="'. $archive_url . '" title="' . __('View ') . $author->display_name . '">';
    	echo get_avatar($author->user_email);
    	echo '<a>';
            echo '<a href="'. $archive_url . '" title="' . __('View ') . $author->display_name . '"><h3>' . $author->display_name . '</h3></a>';
    	echo '</div>';
        }
        echo '</div>';
    } else {
        echo 'No authors found';
    }
    }
    add_shortcode('steering_list', 'steering_list_authors');
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help creating shortcode that queries custom field checkbox’ is closed to new replies.