我的代码:
<?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。 我怎么能这样做?
答案 0 :(得分:0)
以下是我的回答:
$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安装上运行属性。
希望对某人有用!