Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用计数器(Wordpress)按作者~列出类别_Php_Sql_Wordpress_Taxonomy_Author - Fatal编程技术网

Php 使用计数器(Wordpress)按作者~列出类别

Php 使用计数器(Wordpress)按作者~列出类别,php,sql,wordpress,taxonomy,author,Php,Sql,Wordpress,Taxonomy,Author,这是我的密码。它给出了一个特定作者发表过的类别列表。然而,我非常希望在类别名称旁边有一个数字,告诉我作者在不同类别中发表了多少篇文章。有人知道把戏吗?谢谢 <?php $author = get_query_var('author'); $categories = $wpdb->get_results(" SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description FROM

这是我的密码。它给出了一个特定作者发表过的类别列表。然而,我非常希望在类别名称旁边有一个数字,告诉我作者在不同类别中发表了多少篇文章。有人知道把戏吗?谢谢

<?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="<?php echo get_category_link( $category->ID ); ?>" title="<?php echo $category->name ?>">
            <?php echo $category->name.' '.$category->description; ?>
        </a>
    </li>
    <?php endforeach; ?>
</ul>

SQL中的
存在
count()
函数,该函数可以对多行进行计数。在您的情况下,我们需要帖子的数量,因此我们可以使用
COUNT(posts.id)
,如下所示:

<?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="<?php echo get_category_link( $category->ID ); ?>" title="<?php echo $category->name ?>">
            <?php echo $category->name.'('.$category->count.') '.$category->description; ?>
        </a>
    </li>
    <?php endforeach; ?>
</ul>

我想出来了。。。必须运行单独的SELECT函数:一个用于获取类别列表,然后在该循环中再运行一个函数来计算类别中有多少个条目。我更愿意将这两个循环作为一个循环,但这对我来说是可行的

<?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; 

?>
我通常使用此插件()在侧边栏中显示。
我想这段代码对任何人都有帮助

  <?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);

  }
 ?>

已关闭,但仍不完全关闭。您的代码只生成了一行(一个类别),计数器似乎适用于该类别中的所有帖子,而不仅仅是特定作者编写的帖子:(
  <?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);

  }
 ?>