【问题标题】:Get newest 5 WordPress posts per category每个类别获取最新的 5 个 WordPress 帖子
【发布时间】:2016-12-25 14:22:21
【问题描述】:

我想在外部应用程序中为每个类别获取最新的 5 个已发布的 WordPress 帖子。这些应用程序是在 ASP.NET 中构建的,所以我想使用 EF 直接从 MySQL 数据库中获取数据。在 EF 中构建 WordPress 关系似乎很糟糕,所以我想构建一个自定义模型,其中包含有关最新帖子的类别详细信息和信息。

所以我写了一个有效的 SQL 查询,但是因为它的聚合组只给了我每个类别的一行(帖子):

Select terms.term_id As Id, terms.name As Name, terms.slug As SeoName, Group_Concat(relations.object_id) As PostId, taxonomy.count As Count, taxonomy.parent As ParentId,
(
    Select post_title
    From wp_posts 
    Where ID In(Group_Concat(relations.object_id))
    Order By post_date Desc
) As PostTitle
From wp_terms terms, wp_term_taxonomy taxonomy, wp_term_relationships relations
Where terms.term_id = taxonomy.term_id
And relations.term_taxonomy_id = taxonomy.term_taxonomy_id
And taxonomy.taxonomy = 'category'
Group By terms.term_id

我的想法是添加 Having Count(terms.term_id) = 5 以便每个类别有 5 行。但它不起作用,我得到了一个空的结果。如果没有Having,我会在每个类别中获得一条带有最新帖子的行。

如何告诉 MySQL 每个类别返回 5 行而不删除分组?

假设我有树类别:

  • CatA
  • 猫B
  • CatC

那我想从数据库中得到如下结果(简而言之):

CategoryId | CategoryName | NewestPostTitle

1          | CatA     | Newest post in CatA
1          | CatA     | Second newest post in CatA
...
2          | CatB     | Newest post in CatB
2          | CatB     | Second newest post in CatB
...
3          | CatC     | Newest post in CatC
3          | CatC     | Second newest post in CatC

【问题讨论】:

  • 如果您愿意,请考虑遵循以下简单的两步操作: 1. 如果您还没有这样做,请提供适当的 CREATE 和 INSERT 语句(和/或 sqlfiddle),以便我们可以更容易复制问题。 2. 如果您尚未这样做,请提供与步骤 1 中提供的信息相对应的所需结果集。
  • 你为什么不用简单的 WP_Query(或 get_posts())遍历类别,并通过每次迭代查询帖子?

标签: mysql sql wordpress posts


【解决方案1】:

您为什么不使用内置的 wordpress 功能?请尝试以下代码,我已经对其进行了测试,并且可以正常运行:

$categories = get_categories();

/* echo '<pre>';
print_r($categories);
echo '</pre>'; */

foreach($categories as $category){
    $args = array(
        'posts_per_page' => 5,
        'cat' => $category->term_id,
        'orderby' => 'ID',
        'order' => 'DESC'
    );

    $query = new WP_Query($args);

    echo $category->name . '<hr />';

    while($query->have_posts()):$query->the_post();
        echo the_title() . '<br />';
    endwhile;
}

编辑:我没有意识到您想在外部应用程序中使用它。我将使用我提供的代码并创建一个页面模板,该模板将以数组或 json 格式返回结果。

您还可以回显 wordpress 用来获取数据并以此为基础的查询。您可以通过回显 $query->request;

来做到这一点

例如

通过以下方式获取 wordpress 类别列表:

SELECT t.term_id AS id
FROM   wp_terms t
       LEFT JOIN wp_term_taxonomy tt
              ON t.term_id = tt.term_id
WHERE  tt.taxonomy = 'category'
ORDER  BY name

遍历结果并从 term_id 中获取最新的 5 个条目:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (5) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.ID DESC LIMIT 0, 5

用循环中的相应 ID 替换 wp_term_relationships.term_taxonomy_id IN (6)

【讨论】:

    猜你喜欢
    • 2011-03-14
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多