• Resolved kissatkoiria

    (@kissatkoiria)


    Example I’m trying to do something like in example (see link) but for authors. So when user has logged in she can select authors to ‘follow’ or ‘subscribe’ from user preferences. I’m looking for something like in Google News where user can select categories with sliders but only for authors. Where should I start to make it? It can be done with checkboxes.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter kissatkoiria

    (@kissatkoiria)

    function select_authors_fields( $user ) {
        $authors = get_users();
    	$user_tags = get_the_author_meta( 'select_authors', $user->ID );
        ?>
        <table class="form-table">
            <tr>
                <th>Select authors:</th>
                <td>
            <?php
            if ( count( $authors ) ) {
                foreach ($authors as $author) {
    				 if (user_can($author->ID, 'publish_posts')) { ?>
                <p><label for="select_authors_<?php echo esc_attr( $author->ID); ?>">
                    <input
                        id="select_authors_<?php echo esc_attr( $author->ID); ?>"
                        name="select_authors[<?php echo esc_attr( get_the_author_meta('display_name', $author->ID) ); ?>]"
                        type="checkbox"
                        value="<?php echo esc_attr( get_the_author_meta('ID', $author->ID) ); ?>"
                        <?php if ( in_array( get_the_author_meta('ID', $author->ID), $user_tags ) ) echo ' checked="checked"'; ?> />
                    <?php echo esc_html(get_the_author_meta('display_name', $author->ID)); ?>
                </label></p><?php
    				 }
    			 }
            } ?>
                </td>
            </tr>
        </table>
        <?php
    		}
    add_action( 'show_user_profile', 'select_authors_fields' );
    add_action( 'edit_user_profile', 'select_authors_fields' );
    
        // store authors
        function select_authors_fields_save( $user_id ) {
            if ( !current_user_can( 'edit_user', $user_id ) )
                return false;
            update_user_meta( $user_id, 'select_authors', $_POST['select_authors'] );
    	}
        add_action( 'personal_options_update', 'select_authors_fields_save' );
        add_action( 'edit_user_profile_update', 'select_authors_fields_save' );

    Now the author list appears with checkboxes and it saves my selections. How do I write the query to show the posts from authors I have selected?

    Thread Starter kissatkoiria

    (@kissatkoiria)

    function my_get_posts( $query ) {
        if ( !is_user_logged_in() ) return;
        $current_user = wp_get_current_user();
    
        $authors = get_user_meta( $current_user->ID, 'select_authors', true );
    	echo $authors;
    	//$query = new WP_Query( 'author_name=' .$authors );
    	return $query;
    }
    add_filter( 'pre_get_posts', 'my_get_posts' );

    If I only echo $authors it prints many Arrays. Someone?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘User can select authors from user preferences’ is closed to new replies.