【问题标题】:Wordpress get posts with empty custom fieldWordpress 获取带有空自定义字段的帖子
【发布时间】:2012-07-22 23:12:26
【问题描述】:

我有这个查询来获取带有自定义字段的帖子,效果很好。

<?php query_posts('meta_key=version&meta_value=one');  ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
// my posts displaying 

现在我想获取没有 meta_value 为“one”或没有设置版本 meta_key 的帖子。我尝试使用空字符串 'meta_key=&meta_value=' 但它返回所有帖子,包括将 meta_value 设置为 'one' 的帖子。

感谢您的帮助

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    使用$the_query = WP_Query($params)。它返回一个可以像这样使用的查询对象:-

    while ( $the_query->have_posts() ) : $the_query->the_post();
        echo '<li>';
        the_title();
        echo '</li>';
    endwhile;
    
    $params = array(
            'meta_query' =>array(
    
                                    array(
                                            'key' => 'version',
                                            'value' =>'one',
                                            'type' => 'char',
                                            'compare' => 'NOT'),
                                ));
    
    $the_query = new WP_Query($params);
    

    further information on what operators you can use here

    【讨论】:

    • 谢谢,我会检查的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多