2017-01-23 89 views
2

我有一个自定义查询,我想要一些帮助转换为可视化作曲家的自定义查询。基本上,我想排除在帖子网格中显示的具有meta_key:_is_featured_posts及其值为yes的所有帖子。视觉作曲家自定义查询 - 不包含meta_key

// WP_Query arguments 
$args = array(
    'post_type'    => array('post'), 
    'post_status'   => array('publish'), 
    'nopaging'    => false, 
    'posts_per_page'   => '12', 
    'order'     => 'DESC', 
    'orderby'    => 'date', 
    'meta_query'    => array(
     'relation' => 'AND', 
     array(
      'key'  => '_is_ns_featured_post', 
      'value' => 'yes', 
      'compare' => 'NOT EXISTS', 
     ), 
    ), 
); 

// The Query 
$query = new WP_Query($args); 

任何帮助,将不胜感激。

感谢

+0

我需要与最终转换回[]是有点像如下:post_type =包装及包装标签=溢价&post_status =发布。如在这篇文章中:\t design33.net/how-to-visual-composers-grid-custom-query –

+0

[可视化作曲家Wordpress查询为后网格]可能的副本(http://stackoverflow.com/questions/30653046/visual-composer-wordpress-query-post-grid) –

回答

0

有一个替代的解决方案,但不建议但NOT EXISTS不工作,所以你可以使用下面的代码。我也检查给出的解决方案here,但它也不起作用。

//to hold the post id which has _is_ns_featured_post => 'yes' 
$exclude_id = array(); 

$args_exclude = array(
    'post_type' => array('post'), 
    'post_status' => array('publish'), 
    'posts_per_page' => '-1', 
    'meta_query' => array(
     array(
      'key' => '_is_ns_featured_post', 
      'value' => 'yes', 
     ), 
    ), 
); 

$exclude_posts = new WP_Query($args_exclude); 
if (!empty($exclude_posts->posts)) 
{ 
    foreach ($exclude_posts->posts as $post) 
    { 
     $exclude_id[] = $post->ID; 
    } 
} 


$args = array(
    'post_type' => array('post'), 
    'post_status' => array('publish'), 
    'nopaging' => false, 
    'posts_per_page' => '12', 
    'order' => 'DESC', 
    'orderby' => 'date', 
    'post__not_in' => $exclude_id //exclude post_id which has _is_ns_featured_post => 'yes' 
); 

// The Query 
$query = new WP_Query($args); 
foreach ($query->posts as $post) 
{ 
    print_r($post); 
} 

希望这会有所帮助!

+0

感谢您的输入。但我最终需要的是如下所示:post_type = package&package-tag = premium&post_status = publish –

+0

正如以下链接所述: –

+0

https://www.design33.net/how-to-visual-作曲家网定制查询/ –

0

参见:visual composer wordpress query for post grid

试试这个:

$args = array(
    'post_type'    => array('post'), 
    'post_status'   => array('publish'), 
    'nopaging'    => false, 
    'posts_per_page'   => '12', 
    'order'     => 'DESC', 
    'orderby'    => 'date', 
    'meta_query'    => array(
     'relation' => 'AND', 
     array(
      'key'  => '_is_ns_featured_post', 
      'value' => 'yes', 
      'compare' => 'NOT EXISTS', 
     ), 
    ), 
); 

echo http_build_query($args); 

//结果:

post_type%5B0%5D=post&post_status%5B0%5D=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query%5Brelation%5D=AND&meta_query%5B0%5D%5Bkey%5D=_is_ns_featured_post&meta_query%5B0%5D%5Bvalue%5D=yes&meta_query%5B0%5D%5Bcompare%5D=NOT+EXISTS 

http://sandbox.onlinephpfunctions.com/code/5c2bc6ddd37a02fc8facf4f227176e262854b92e

我会建议避免使用阵列( '后')的如果只有一个帖子类型,那么只需使用post_type=post&post_status=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query[relation]=and&meta_query[0][key]=_is_ns_featured_post&meta_query[0][value]=yes&meta_query[0][compare]=NOT EXISTS

P.S. 可能%5B%5D您需要通过echo urldecode(http_build_query($args));