2

我有一个自定义查询,我需要一些帮助来转换为可视化作曲家的自定义查询。基本上,我想从帖子网格中排除所有帖子,这些帖子具有 meta_key: _is_featured_posts 并且其值为 yes。

// 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 );

任何帮助,将不胜感激。

谢谢

4

2 回答 2

0

请参阅:用于后网格的可视化作曲家 wordpress 查询

尝试这个:

$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

如果只有一种帖子类型,我建议避免使用 array('post') ,所以只需使用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

PS 可能%5B%5D您需要转换回[]通过echo urldecode(http_build_query($args));

于 2017-01-24T07:35:26.783 回答
0

有一个替代解决方案,不推荐,但由于NOT EXISTS不起作用,因此您可以使用以下代码。我也检查了这里给出的解决方案,但它也不起作用。

//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);
}

希望这可以帮助!

于 2017-01-23T07:30:21.097 回答