如何在 WordPress 中获取某些帖子

发布于 2024-12-20 10:28:28 字数 849 浏览 2 评论 0原文

我想实现一个功能来获取 WordPress 中的某些帖子。

INPUT

page_number

Category_name

VARIABLE

items_per_page = 10

OUTPUT

posts 数组,类似于 query_posts() 函数的结果。

这是我的代码:

$page_number = $_GET["page_number"]
$category_name = $_GET["category_name"]

function app_get_posts($page_number, $category,$items_per_page = 10)
{
    global $wpdb;  
    $select ="SELECT POSTS FROM wp_posts  WHERE CATEGORY = ".$category." LIMIT (".$page_number." - 1) * ".$items_per_page.",".$page_number."  * ".$items_per_page; //it didn't work.
    return $wpdb->query($select); 
}

当我调用函数app_get_posts('2','tech')时,它将返回“tech”类别中的第10~19篇帖子,当我调用app_get_posts('3','wordpress')时,它将返回“wordpress”类别中的第 20~29 个帖子。

所以我想知道是否有办法解决这个问题。 提前谢谢。

I want to implement a function to get certain posts in wordpress.

INPUT

page_number

category_name

VARIABLE

items_per_page = 10

OUTPUT

posts array, like the result of the query_posts() function.

Here is my code:

$page_number = $_GET["page_number"]
$category_name = $_GET["category_name"]

function app_get_posts($page_number, $category,$items_per_page = 10)
{
    global $wpdb;  
    $select ="SELECT POSTS FROM wp_posts  WHERE CATEGORY = ".$category." LIMIT (".$page_number." - 1) * ".$items_per_page.",".$page_number."  * ".$items_per_page; //it didn't work.
    return $wpdb->query($select); 
}

When I call the function app_get_posts('2','tech'), it will return the 10th~19th posts in the "tech" category, when I call app_get_posts('3','wordpress'), it will return the 20th~29th posts in the "wordpress" category.

So I am wondering if there is a way to figure out this problem.
Thx in advence.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ左 2024-12-27 10:28:28

我猜上面的方法不起作用的原因是 wp_posts 表没有 CATEGORYPOSTS 字段。我修改了您的代码,以便您可以根据分类返回帖子:

function app_get_posts($page_number, $category,$items_per_page = 10)
{
   global $wpdb;  
   $start = ($page_number-1) * $items_per_page;
   $select ="SELECT DISTINCT wp.* FROM wp_posts wp  
             INNER JOIN wp_term_relationships rs ON rs.object_id = wp.ID 
             INNER JOIN wp_terms t ON t.term_id = rs.term_taxonomy_id 
             WHERE t.name = ".$category." LIMIT ".$start.",".$items_per_page;
   return $wpdb->query($select); 
 }

I guess the reason why the above won't work is because the wp_posts table not has a CATEGORY or POSTS field. I modified you code so you can return posts based on taxonomy:

function app_get_posts($page_number, $category,$items_per_page = 10)
{
   global $wpdb;  
   $start = ($page_number-1) * $items_per_page;
   $select ="SELECT DISTINCT wp.* FROM wp_posts wp  
             INNER JOIN wp_term_relationships rs ON rs.object_id = wp.ID 
             INNER JOIN wp_terms t ON t.term_id = rs.term_taxonomy_id 
             WHERE t.name = ".$category." LIMIT ".$start.",".$items_per_page;
   return $wpdb->query($select); 
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文