Ottieni tutti i post di un autore specifico

20 mar 2013, 13:23:22
Visualizzazioni: 14.3K
Voti: 3

Sto creando una dashboard back-end dove devo mostrare tutti i post assegnati all'utente corrente dall'amministratore di WordPress.

Assegno il ruolo utente come Autore e durante la creazione del post (come amministratore wp) assegno semplicemente il post a un autore dal menu a tendina Autori.

Quindi ho bisogno di mostrare i post con stato Pubblicato. Attualmente sto usando una semplice query sui post ma restituisce tutti i post.

global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;    // per l'utente corrente è 2

$query = array(
        'post_type' => 'post',
        'post_author' => $user_id,
        'post_status' => array('publish')
    );
$my_posts = query_posts($query);

Ho anche inserito manualmente post_author a 2

Ho anche provato $my_post = new WP_Query(array( 'post_author' => '2' ));

ma senza successo.

0
Tutte le risposte alla domanda 3
0

La risposta più breve possibile sarebbe correggere 'post_author' in 'author', poiché questa è la chiave che WordPress cerca. Se una chiave è errata o scritta male verrà ignorata, come nel caso di 'post_author'.

1 mag 2015 14:49:05
1

Grazie a [Sheikh Heera][1]

if ( is_user_logged_in() ):
    global $current_user;
    get_currentuserinfo();
    $author_query = array(
        'posts_per_page' => '-1', // Numero di post da visualizzare (-1 per tutti)
        'author' => $current_user->ID // ID dell'autore corrente
    );
    $author_posts = new WP_Query($author_query);
    while($author_posts->have_posts()) : $author_posts->the_post();
?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php           
    endwhile;
else :

    echo "utente non loggato";
endif;


  [1]: https://stackoverflow.com/users/741747/sheikh-heera
20 mar 2013 14:01:30
Commenti

Meh. In qualche modo ho perso il fatto che avessi già una soluzione :/

kaiser kaiser
20 mar 2013 14:49:46
0

Il seguente mini-plugin aggiunge un widget nella dashboard che mostra i post dell'utente corrente con stato publish. Puoi vedere get_current_user_id() in azione.

<?php
defined( 'ABSPATH' ) OR exit;
/**
 * Plugin Name: (#91605) Dashboard Widget - User posts
 */

add_action( 'wp_dashboard_setup', 'wpse91605_dbwidget_user_posts' );
function wpse91605_dbwidget_user_posts()
{
    wp_add_dashboard_widget(
         'wpse91605_dbwidget_user_posts'
        ,_e( 'I tuoi post pubblicati', 'your_textdomain' )
        ,'wpse91605_dbwidget_user_posts_cb'
    );
}
function wpse91605_dbwidget_user_posts_cb()
{
    $query = new WP_Query( array(
         'author'         => get_current_user_id()
        ,'post_status'    => 'publish'
        ,'posts_per_page' => -1
        ,'showposts'      => -1
        ,'nopaging'       => true
    ) );
    if ( $query->have_posts() )
    {
        ?><ul><?php
        while( $query->have_posts )
        {
            the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_title(); ?>
            </a></li>
            <?php
        }
        ?></ul><?php
    }
}
20 mar 2013 14:49:31