Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

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

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

1  
o great thanks. it is the script which i need. –  Muhammad Sajid Mar 20 '13 at 12:00
    
You are welcome :-) –  The Alpha Mar 20 '13 at 12:01

2 Answers 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;
share|improve this answer
    
Meh. Somehow I missed that you already had a solution :/ –  kaiser Mar 20 '13 at 12:49

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
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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