WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How can i list the author of the category? (9 posts)

  1. genxer
    Member
    Posted 1 year ago #

    Hello all,
    How can i list the author of the category?
    For example;
    CAT NAME : INTERNET
    AUTHOR: JOHN, DOE, ALEX
    CAT NAME: TECH
    AUTHOR: JOHN
    CAT NAME: CODE
    AUTHOR: ALEX

    Regards

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I think this will do what you want:

    <?php
    $sql = "
    SELECT DISTINCT t.name as cat_name, um1.meta_value AS first_name, um2.meta_value AS last_name
    FROM $wpdb->terms t
    JOIN $wpdb->term_taxonomy tt ON (tt.term_id = t.term_id AND taxonomy = 'category')
    JOIN $wpdb->term_relationships tr ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
    JOIN $wpdb->posts p ON p.ID = tr.object_id
    JOIN $wpdb->usermeta um1 ON (um1.user_id = p.post_author AND um1.meta_key = 'first_name')
    JOIN $wpdb->usermeta um2 ON (um2.user_id = p.post_author AND um2.meta_key = 'last_name')
    WHERE p.post_type = 'post'
    AND p.post_status IN ('publish','private')
    AND p.post_date <= NOW()
    ORDER BY cat_name, first_name, last_name
    ";
    $rows = $wpdb->get_results($sql);
    // Now output category name person1, person2, ...
    $curr_cat = '';
    foreach ($rows as $row) {
       if ($row->cat_name != $curr_cat) {
          if ($curr_cat !== '') echo "</p>\n</div>\n"; // Close previous div
          echo '<div class="cat_div">';        // Start new div
          $curr_cat = $row->cat_name;
          echo "CAT NAME: $row->cat_name";
          echo "<p>AUTHOR: ";
          $sep = '';
       }
       echo "$sep$row->first_name $row->last_name";
       $sep = ', ';
    }
    echo "</p>\n</div>\n"; // Close previous div
    ?>

    Put this code in a template styled for your theme.

  3. genxer
    Member
    Posted 1 year ago #

    Worked! Many thanks

    But when i use it in my sidebar php widget, its not worked.
    I see this error:

    Fatal error: Call to a member function get_results() on a non-object in yourwebsite.com\plugins\exec-php\includes\runtime.php(42) : eval()'d code on line 15

    How can i fix it?

    Regards

  4. vtxyzzy
    Member
    Posted 1 year ago #

    Try changing this:

    <?php
    $sql = "

    to this:

    <?php
    global $wpdb;
    $sql = "
  5. genxer
    Member
    Posted 1 year ago #

    Worked!. Thanks

  6. vtxyzzy
    Member
    Posted 1 year ago #

    If your problem has been solved, please use the dropdown on the right to mark this topic 'Resolved' so that anyone else with this question can see that there is a solution.

  7. genxer
    Member
    Posted 1 year ago #

    @vtzyzzy i have a question again.

    This code listed authors category its worked but its not worked same php code widget.i see same error.

    CODE:

    <?php global $post; $wpdb;
    $author_id = $post->post_author;
    $categories = $wpdb->get_results("
    	SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug
    	FROM $wpdb->posts as posts
    	LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
    	LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
    	LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
    	WHERE 1=1 AND (
    		posts.post_status = 'publish' AND
    		posts.post_author = '$author_id' AND
    		tax.taxonomy = 'category' )
    	ORDER BY terms.name ASC
    ");
    
    echo '<ul>';
    foreach($categories as $category) :
    echo'<li>';
    echo '<a href="';
    echo get_category_link( $category->ID );
    echo'" title="';
    echo $category->name;
    echo'">';
    echo $category->name;
    echo '</a>';
    echo'</li>';
    endforeach;
    echo'<br />';
    echo'</ul>';
    ?>
  8. vtxyzzy
    Member
    Posted 1 year ago #

    The first semi-colon in this line:

    <?php global $post; $wpdb;

    should be a comma:

    <?php global $post, $wpdb;
  9. genxer
    Member
    Posted 1 year ago #

    Yes its worked. Thank you for all your help.

Topic Closed

This topic has been closed to new replies.

About this Topic