
2berühmt.TV
Das ist der Code, den ich habe. Es gibt eine Liste der Kategorien, in denen ein bestimmter Autor veröffentlicht hat. Ich hätte jedoch gerne eine Nummer neben dem Kategorienamen, die angibt, wie viele Beiträge der Autor in den verschiedenen Kategorien veröffentlicht hat. Kennt jemand einen Trick? Danke!
<?php
$author = get_query_var('author');
$categories = $wpdb->get_results("
SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description
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" AND
tax.taxonomy = 'category' )
ORDER BY terms.name ASC
");
?>
<ul>
<?php foreach($categories as $category) : ?>
<li>
<a href="https://stackoverflow.com/questions/17408347/<?php%20echo%20get_category_link(%20$category->ID%20);%20?>" title="<?php echo $category->name ?>">
<?php echo $category->name.' '.$category->description; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
BEARBEITEN:
Dieser Code zählt die Beiträge in der Kategorie und funktioniert gut. Ich möchte dies mit dem obigen Code kombinieren, aber ich weiß nicht, wie es geht …
<?php
$counter = "SELECT COUNT(*)
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = 412
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status="publish"
AND post_author="1"
";
$user_count = $wpdb->get_var($counter);
echo $user_count;
?>
Ich habe es herausgefunden … musste zu separaten SELECT-Funktionen laufen: eine, um die Liste der Kategorien abzurufen, und dann eine weitere Funktion innerhalb dieser Schleife, um zu zählen, wie viele Einträge es in der Kategorie gibt. Ich hätte es vorgezogen, diese beiden Schleifen als eine zu haben, aber das funktioniert für mich.
<?php
// This will get us a list of the categories that our Author has published in
$author = get_query_var('author');
$categories = $wpdb->get_results("
SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description
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 posts.post_status="publish" AND
posts.post_author="$author" AND
tax.taxonomy = 'category'
ORDER BY terms.name ASC
");
// This loop picks up categories
foreach($categories as $category) :
$catid = $category->ID;
// Now, inside the loop, we need to count how many posts that the Author has published.
$counter = "SELECT COUNT(*)
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = $catid
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status="publish"
AND post_author="$author"
";
$user_count = $wpdb->get_var($counter);
echo '<div class="archive_author">' . $category->name . '<br/><span class="subcounter">' . $user_count . ' posts</span></div>';
endforeach;
?>
In SQL existiert die count()
Funktion, die eine Anzahl von Zeilen zählen kann. In Ihrem Fall möchten wir die Anzahl der Beiträge, die wir verwenden könnten COUNT(posts.id)
so:
<?php
$author = get_query_var('author');
$categories = $wpdb->get_results("
SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description, count(posts.id) AS `count`
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 posts.post_status="publish" AND
posts.post_author="$author" AND
tax.taxonomy = 'category'
ORDER BY terms.name ASC
");
?>
<ul>
<?php foreach($categories as $category) : ?>
<li>
<a href="https://stackoverflow.com/questions/17408347/<?php%20echo%20get_category_link(%20$category->ID%20);%20?>" title="<?php echo $category->name ?>">
<?php echo $category->name.'('.$category->count.') '.$category->description; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
Sie werden feststellen, dass ich und Alias verwendet habe, um die count-Spalte umzubenennen (andernfalls wäre ihr Name gewesen count(posts.id)
– nicht wirklich praktisch). Die habe ich auch entfernt 1=1
im WHERE, weil es hier nicht sinnvoll ist.

Sabir Abdul Gafoor Shaikh
Normalerweise verwende ich dieses Plugin ( http://wordpress.org/plugins/author-profiles/ ) zur Anzeige in der Seitenleiste. Und ich denke, dieser Code wird jedem helfen
<?php
global $wpdb;
$table_users.=$wpdb->base_prefix;
$table_users.="users";
$table_posts.=$wpdb->base_prefix;
$table_posts.="posts";
$fetch_authordata="SELECT count(p.post_author) as post1,c.id, c.user_login, c.display_name,c.user_nicename, c.user_email, c.user_url, c.user_registered FROM {$table_users} as c , {$table_posts} as p {$where} and p.post_type="post" AND p.post_status="publish" and c.id=p.post_author GROUP BY p.post_author order by post1 DESC limit {$author_numbers} ";
$dispaly_authordata = (array) $wpdb->get_results("{$fetch_authordata}", object);
foreach ( $dispaly_authordata as $author ) {
$user = get_userdata($author->id);
echo 'Display Name: '.$user->display_name;
echo 'Post Count: '.$post_count = get_usernumposts($user->ID);
}
?>
10049300cookie-checkKategorien nach Autor auflisten ~ WITH COUNTER ~ (WordPress)yes