WordPress.org

Ready to get started?Download WordPress

Forums

How can i list posts only from Authors, excluding admins users. (24 posts)

  1. wasferraz
    Member
    Posted 3 years ago #

    Hi to all!

    Please, how can i list posts only from Authors, excluding Admins users?

    I want to create a page to list latest posts only from Authors users and each other only to list posts from Contributors users. An important thing is that in that pages I don't want do list Admins users posts.

    Please, help-me.

    Many thanks!

  2. aphill70
    Member
    Posted 3 years ago #

    Outside the loop this should work,

    <?php
    global posts;
    
    $nonAdminPosts = get_posts(array('author' => -1, ...));
    
    ?>

    This will work outside the loop and you can add whatever arguments you want to.

    http://codex.wordpress.org/Template_Tags/get_posts
    http://codex.wordpress.org/Function_Reference/query_posts

    The other option would be to alter the loop query using query_posts.

    This will work to remove specific users from the list not a user role

  3. wasferraz
    Member
    Posted 3 years ago #

    Hi alphill70, thanks for response!

    Returns this error when trying the fisrt code, that is exactly what i need, outside the loop.

    The erro:
    Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in C:\xampp\htdocs\demos\wp-content\themes\informecidade\home-content.php on line 92

    Any idea?

  4. aphill70
    Member
    Posted 3 years ago #

    the first line should be

    global $post;

    I left off the $ sign and there shouldn't be an s

    my bad

  5. wasferraz
    Member
    Posted 3 years ago #

    Not bad, I do the change and get new error:

    `Parse error: syntax error, unexpected '.', expecting ')' in C:\xampp\htdocs\demos\wp-content\themes\informecidade\home-content.php on line 94'

    then I try to correct it with the follow:

    <?php
    global $posts;
    $nonAdminPosts = get_posts(array('author' => -1, '...'));
    ?>

    and It returns no error, but don't show anything.

    Help me rssss

    Thank you

  6. aphill70
    Member
    Posted 3 years ago #

    to display you will need this

    <?php
    global $post;
    $nonAdminPosts = get_posts(array('author' => -1));
    
    foreach($nonAdminPosts as $nonAdmin):
    echo $nonAdmin->post_title;
    endforeach;
    ?>

    sorry i was leaving the dots so you could add more arguments.

    This should now output a list of the titles.

    Hopefully i have removed all the errors but let me know if i missed something

  7. wasferraz
    Member
    Posted 3 years ago #

    You are the man hehe, functions great!

    But i also need to display the title, the name of the author, the date when the post was written and the link to the post. And I need to show 2 registers only.

    Is it asking to much ask your help in this question too? If yes I will try to search how to implement this extras.

    Thank you very much!

  8. wasferraz
    Member
    Posted 3 years ago #

    The variable &nonAdmin can be used to show post_linklik, post_thumbnail and post_author like in post_title ?

    Is that correct?

  9. aphill70
    Member
    Posted 3 years ago #

    yeah that is correct if you just use the same basic format as i did then you should be able to print whatever you want.

    a reference for the indexes is here.

    http://www.rlmseo.com/blog/wordpress-post-variable-quick-reference/

    use the text from the first column of the table on that page
    to display the link use

    echo "<li><a href=\" " . get_permalink() . "\">$nonAdmin->post_title</a></li>";
    
    echo get_the_post_thumbnail( $noAdmin->ID );
    
    the_author_meta( 'user_nicename', $noAdmin->ID );

    these when placed within the foreach should output what you are looking for i hope. not one hundred percent sure since a couple of these i just kind of dug up.

  10. wasferraz
    Member
    Posted 3 years ago #

    ok thanks a lot, I'll break my head a little now rsss

  11. wasferraz
    Member
    Posted 3 years ago #

    Woww an new problem:

    The permanlink and the code above get the information from other post. Only the title show the right information.

    `
    echo get_the_post_thumbnail( $noAdmin->ID );
    the_author_meta( 'user_nicename', $noAdmin->ID );
    `

  12. aphill70
    Member
    Posted 3 years ago #

    you dropped that code into the foreach loop?

    <?php
    global $post;
    $nonAdminPosts = get_posts(array('author' => -1));
    
    foreach($nonAdminPosts as $nonAdmin):
    echo $nonAdmin->post_title;
    echo "<li><a href=\" " . get_permalink($nonAdmin->ID) . "\">$nonAdmin->post_title</a></li>";
    
    echo get_the_post_thumbnail( $noAdmin->ID );
    
    the_author_meta( 'user_nicename', $noAdmin->ID );
    endforeach;
    
    ?>

    this is what it should look like if that is what you have and it isn't working please post the entire code you have...for this chunk

  13. wasferraz
    Member
    Posted 3 years ago #

    Hey man, let's finish this? lack a little!

    Well was missing the letter "n" in the spelling of the variable "nonAdmin" in some places. I fix it and work.

    Now the thumbnail is being displayed, the title and date also are displayed correctly.

    Isn't correctly displayed the name of the author.

    And permanlink is pointing to the wrong place.

    The code is above:

    <?php
    	global $post;
    	$nonAdminPosts = get_posts(array('author' => -1));
    	foreach($nonAdminPosts as $nonAdmin):
    	echo "
    
    <li><a href=" ">$nonAdmin->post_title</a></li>
    	";
    	echo get_the_post_thumbnail( $nonAdmin->ID, 'thumb-size' );
    	the_author_meta( 'user_nicename', $nonAdmin->ID );
    	echo $nonAdmin->post_date;
    	endforeach;
    ?>
  14. aphill70
    Member
    Posted 3 years ago #

    echo "<a href=\"$nonAdmin->guid\"></a>";

    I believe that the guid field the $nonAdmin object will link to the correct post.

    I have used the permalink method before so i am not sure why its not working let me know if that works

  15. wasferraz
    Member
    Posted 3 years ago #

    echo "<a href=\"$nonAdmin->guid\"></a>";
    the link code above function very well and link to correct place,
    the only thing is that in the status bar it shows the link structure type like: http://www.mysite.com/?p=139 - but it doesn't matter.

    And about the author's name that don't show? You have any idea?

  16. aphill70
    Member
    Posted 3 years ago #

    on the user meta piece change ID to author

    That should fix the problem

  17. wasferraz
    Member
    Posted 3 years ago #

    the_author_meta( 'user_nicename', $nonAdmin->author );

    like this dont solve the problem.

    ;(

  18. aphill70
    Member
    Posted 3 years ago #

    try changing user_nicename to user_login

    http://codex.wordpress.org/Function_Reference/the_author_meta

    here is the reference for the function

    My best guess is that you don't have a nicename set.

    anyways hope this helps

  19. wasferraz
    Member
    Posted 3 years ago #

    aphill70 thanks for all your help man!

  20. wasferraz
    Member
    Posted 3 years ago #

    aphill70, the author name don't show with any option in the codex.

    And how i do to show only 2 posts?

    Thanks!

  21. aphill70
    Member
    Posted 3 years ago #

    ok i think i figured out why it wasn't working earlier with the permalink and i think i figured out how to fix it.

    change $nonAdmin to $post, the the get_the_author(); should work, as would the permalink function.

    echo get_the_author();
  22. aphill70
    Member
    Posted 3 years ago #

    You need to change the $nonAdmin to $post.

    then the functino get_the_author() should work.

    echo get_the_author();
  23. wasferraz
    Member
    Posted 3 years ago #

    The changes have not worked, I think is becouse I don't know nothing about PHP. ;( rsss

  24. aphill70
    Member
    Posted 3 years ago #

    Sorry this has taken me so long to get back to you if you will post the code you are using for this chunk that will be the easiest way for me to troubleshoot the php

Topic Closed

This topic has been closed to new replies.

About this Topic