• I’m trying to make every post only available to their authors, so nobody else can see or edit them, expect for the site’s admin. And I finally got it thanks to the following code that I found on this site:

    function posts_for_current_author($query) {
    	global $pagenow;
    
    	if( 'edit.php' != $pagenow || !$query->is_admin )
    	    return $query;
    
    	if( !current_user_can( 'manage_options' ) ) {
    		global $user_ID;
    		$query->set('author', $user_ID );
    	}
    	return $query;
    }
    add_filter('pre_get_posts', 'posts_for_current_author');

    The thing is that I can’t find out how to modify the posts count for every author, because it still shows the total amount of posts published on my site.

    Any ideas? Thanks in advance ??

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

    (@bcworkz)

    The query object knows how many posts it returned. You can retrieve it with $query->post_count . Assign it to another variable before reseting the query, then have the template display that value instead of whatever it is doing now.

    That sounds like a good idea, but I will have to ask you for some help. My knowledge on php, mysql and wordpress’s own programming language is quite limited.
    I really would appreciate it!^^

    Moderator bcworkz

    (@bcworkz)

    Coding out of context is not ideal, I’m guessing at the specifics of your template. Anyway, insert the following in the applicable template right after the “loop”, the part that usually begins with something like <?php while ( have_posts() ) : the_post(); ?> and ends with <?php endwhile; ?>. You may need to remove the <?php ?> portion if the endwhile; is not followed by ?>

    <?php global $wp_query;
    $auth_count = $wp_query->post_count; ?>

    Then replace the portion that displays the count with something like this, probably after the “loop” somewhere (feel free to alter the portion in quotes except for the ‘$auth_count’):
    <?php echo "This author has published $auth_count posts."; ?>

    If you cannot get this working, post the portion of your template containing the “loop” and the post count display on pastebin.com. Post a link here and then I can give you more specific code.

    Before doing all this though, I encourage you to create a child theme to protect your changes from theme upgrades. Simply place your modified template files in your child theme’s folder. The only other required file is a simple style.css file with a certain format outlined in the article.

    I’m sorry but I guess I might not have been clear on my question. I’m not planning on modifying any core file, because that’s what I would have to do if I wanted edit something related to the loop. That’s why I’m trying to find a solution through the functions file.

    And I also forgot to mention that the displayed amount of posts that I was talking about is the one showed in the backend post list

    Moderator bcworkz

    (@bcworkz)

    Well, the info was there if I had paid more attention to your code snippet. I wrongly had it in mind you would be editing a template file. I don’t see a way to change the count on the backend panel without a core hack. Sorry to get your hopes up.

    I truly appreciate your effort! ^^ I’ll have to keep on looking for a solution. There must be one!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display only author's posts’ is closed to new replies.