如果不存在多个元键,则查询所有帖子

时间:2016-04-08 作者:WendiT

我正在尝试获取所有没有以aa\\uUket开头的元ket的帖子。我可以这样做

$args = array(
\'posts_per_page\' => -1,
\'category__in\' => 23,
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
 \'key\' => \'aa_article_1\',
 \'compare\' => \'NOT EXISTS\', 
 \'value\' => \'\' 
),
array(
 \'key\' => \'aa_article_2\',
 \'compare\' => \'NOT EXISTS\', 
 \'value\' => \'\' 
),
array(
 \'key\' => \'aa_article_3\',
 \'compare\' => \'NOT EXISTS\', 
 \'value\' => \'\' 
),
array(
 \'key\' => \'aa_article_4\',
 \'compare\' => \'NOT EXISTS\', 
 \'value\' => \'\' 
),
array(
 \'key\' => \'aa_article_5\',
 \'compare\' => \'NOT EXISTS\', 
 \'value\' => \'\' 
),
array(
 \'key\' => \'aa_article_6\',
 \'compare\' => \'NOT EXISTS\', 
 \'value\' => \'\' 
)

));

$the_query = new WP_Query ($args);
// The Loop
if ( $the_query->have_posts() ) {
echo \'<ul>\';
while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo \'<li><a href="\' . get_the_permalink() . \'" target="_blank">\' . get_the_title() . \'</a></li>\';
}
echo \'</ul>\';
echo "<h2>Found: $the_query->found_posts posts</h2>";
} else {
// no posts found
}
wp_reset_postdata();
有没有办法做到这一点?我有大约50个以aa\\u1开头的元密钥,这样可以节省一些时间。当然,很高兴知道是否有可能,以备将来使用:)

1 个回复
SO网友:Aishan

全球wpdb美元;

$excludeposts=“从$wpdb->postsWHERE$wpdb->posts.post\\u status=\'publish\'AND meta\\u key NOT LIKE%aa\\u%ORDER BY post\\u date DESC”;

$main\\u query=新的WP\\u查询(数组(\'post\\u not\\u in\'=>$excludeposts,\'paged\'=>$paged));

while($main\\u query->have\\u posts()):

$main_query->the_post();
//Stuff...
结束时;

//希望这有帮助

相关推荐

ARRAY_MERGE内的Foreach循环

我需要在下拉菜单中获得用户显示名称的列表。我有查询权限,但我不确定如何准确地将我的用户foreach放入数组中。以下是我需要将用户注入的代码:$data[\'settings\'][\'advanced_options\'] = array_merge($data[\'settings\'][\'advanced_options\'], [ [ "label" => "Dynamic Option 1", //