4

我正在使用 Wordpress。我有以下查询来从数据库中获取数据,它运行良好

$args1 = array(
 'post_type' => 'gallery',
 'posts_per_page' => $gnum,
 'post__in' => array(400, 403),
 'paged' => $paged,
 'orderby' => 'title',
 'order' => 'ASC'                    

);

query_posts($args1);

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

//And then some other code to display data

在上面的查询中使用'post__in' => array(400, 403),我正在获取 ID='400' AND '403' 的行。所以当我回显时,我只能看到两个信息。

现在,我想要实现的是从表中获取所有数据,但是当我显示信息时,我想首先获取 ID 为 400 的行,然后是 403,然后基于'orderby' => 'title',AND 'order' => '升序'

你能帮忙查询一下吗?

谢谢

编辑

$args2 = array(
 'post_type' => 'gallery',
 'posts_per_page' => $gnum,
 'post__not_in' => array(400, 403),
 'paged' => $paged,
 'orderby' => 'title',
 'order' => 'ASC'                    

);

query_posts($args2);
4

3 回答 3

3

不确定这是否会在query_post参数中起作用,但它正在补充有效的 SQL。尝试:

$args = array(
 'post_type' => 'gallery',
 'posts_per_page' => $gnum,
 'paged' => $paged,
 'orderby' => 'case when ID in (400,403) then -1 else title end, title',
 'order' => 'ASC'                    
);

query_posts($args);
于 2013-04-02T01:27:55.947 回答
0

像您这样使用 Wordpress 查询结构是不可能的。一种可能的解决方案是进行第二次查询,查找不在 (400, 403) 中的结果,然后简单地将这个结果数组添加到第一个数组的末尾。

为了做到这一点,您可能应该使用get_posts()而不是query_posts()这样它就不会改变主循环。

$array = get_posts($args1);
$array = array_merge($array, get_posts($args2);
于 2013-04-01T17:32:46.487 回答
0

我想您可以尝试执行以下操作:

$args = array(
 'post_type' => 'gallery',
 'posts_per_page' => $gnum,
 'paged' => $paged,
 'orderby' => 'ID = 400 DESC, ID = 403 DESC, title',
 'order' => 'ASC'                    
);

query_posts($args);
...

让我知道这是怎么回事。

于 2013-04-01T17:50:25.007 回答