WordPress |排序依据和过滤器 - 存档页面



我的代码:

<?php
$the_key = 'upsidedate';  // The meta key to sort on
$args = array(
   'meta_key' => $the_key,
   'orderby' => 'meta_value',
   'order' => 'ASC',
);
global $wp_query;
query_posts(
    array_merge(
        $wp_query->query,
        $args
    )
);
?>
<?php 
if (have_posts()) : while (have_posts()) : the_post() ;
get_template_part('post', 'archive');
endwhile;
else :
    get_template_part('post', 'noresults');
endif; 
get_template_part('navigation');
?>

现在在做什么?它通过自定义字段" upsidedate"的顺序。

我想通过自定义字段" upsidedate" 通过自定义字段"完成"过滤的订购在档案中发帖。

应该是这样的:

$the_query = new WP_Query( array( 'meta_key' => 'done', 'meta_value' => 'no' ) ); 

,但我不能两次使用meta_key。我该怎么做?

这是我的答案:

$the_key = 'upsidedate';
query_posts(array(
        'meta_key' => $the_key,
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'done',
                'value' => 'no',
                'compare' => '='
            )
        )
    ));
 // For test only. Please comment it in production
 echo $GLOBALS['wp_query']->request; die;
 // My query will be:
# SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  
#   INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  
#   INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1  AND (
#   wp_postmeta.meta_key = 'upsidedate'
#   AND
#   (
#     ( mt1.meta_key = 'done' AND CAST(mt1.meta_value AS CHAR) = 'no' )
#   )
# ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') 
#   GROUP BY wp_posts.ID 
#   ORDER BY wp_postmeta.meta_value ASC LIMIT 0, 10
 // End test only
    wp_reset_query();

您可以通过命令再次检查:


更新:

$the_key = 'upsidedate';
query_posts(array(
        'meta_key' => $the_key,
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'done',
                'value' => 'no',
                'compare' => '='
            )
        )
    ));
if (have_posts()) : while (have_posts()) : the_post() ;
    get_template_part('post', 'archive');
endwhile;
else :
    get_template_part('post', 'noresults');
endif;
get_template_part('navigation');
wp_reset_query();

如果仍然不起作用,我认为您可能有一些自定义挂钩更改WP的查询。此代码将在新的WP安装上运行属性。

希望对某人有用!

最新更新