1

I am creating a back-end dashboard where I need to show all the post assigned to current user by the wp admin.

I assign user role to Author and while creating post (as a wp admin) just assign this post to some author from Author drop-down.

So I need to show posts with status Publish. I am now using simple query post but it is returning all posts.

global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;    // for current user it is 2

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

I also hard-coded post_author to 2

I also tried $my_post = new WP_Query(array( 'post_author' => '2' ));

but fail.

migrated from stackoverflow.com Mar 20 '13 at 12:16

This question came from our site for professional and enthusiast programmers.

2

Thanks to Sheikh Heera

if ( is_user_logged_in() ):

    global $current_user;
    get_currentuserinfo();
    $author_query = array('posts_per_page' => '-1','author' => $current_user->ID);
    $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 "not logged in";

endif;
  • Meh. Somehow I missed that you already had a solution :/ – kaiser Mar 20 '13 at 12:49
1

The following mini-plugin adds a dashboard widget that queries posts from the current user that have publish as post status. You can see get_current_user_id() in use.

<?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( 'Your published posts', '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
    }
}
1

The shortest possible answer would be to correct 'post_author' to 'author', since that is the key WP is looking for. If a key is incorrect or misspelled it will be ignored, as was the case with 'post_author'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.