【问题标题】:Check if custom field has a value检查自定义字段是否有值
【发布时间】:2014-02-22 15:26:47
【问题描述】:

我正在使用插件http://www.advancedcustomfields.com 并尝试查询数以千计的自定义帖子。

我有下面的循环,它正在检查帖子是否没有将自定义字段粘性帖子选择为是。

为了使其工作,我必须手动浏览数千个帖子并保存它们,以便保存自定义字段值。

如何添加到此查询以检查自定义字段是否存在值?

$myposts = get_posts(array(
                        'post_type' => 'news',
                        'posts_per_page' => $display,
                        'post_status' => 'publish',
                            'tax_query' => array(
                                array(
                                'taxonomy' => 'topics', 
                                'field' => 'slug', 
                                'terms' => array($title))
                            ),  
                        'meta_query' => array(
                        'relation' => 'OR',
                                array(
                                    'key' => 'sticky_post',
                                    'value' => 'Yes',
                                    'compare' => '!='
                                ),
                                array(
                                    'key' => 'sticky_post',
                                    'compare' => false
                                )                                                           
                        )       
                        ));

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    请尝试使用此代码获取帖子的所有自定义字段

    <?php
    
      $custom_fields = get_post_custom(72);
      $my_custom_field = $custom_fields['my_custom_field'];
      foreach ( $my_custom_field as $key => $value ) {
        echo $key . " => " . $value . "<br />";
      }
    
    ?>
    

    【讨论】:

      【解决方案2】:

      我实际上通过使用 ACF 中的更新功能解决了这个问题 - 所以我更新了所有帖子以使自定义字段值最少,从而减少了查询空白自定义字段的需要。

      $topposts1 = get_posts(array(
                          'post_type' => 'news',
                          'posts_per_page' => 100000,
                          'post_status' => 'publish')); 
      
                          $featured_count = 0;
                          foreach ($topposts1 as $post) {
                          setup_postdata($post);
      
                          $field_key = "field_52a1b8b824fff";
                          $value = "No";
                          $post_id = $post->ID;
                              update_field( $field_key, $value, $post_id);
                          echo $post->post_title;
                          echo "<br />";
                          } wp_reset_query(); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-14
        • 1970-01-01
        • 2017-09-30
        • 2011-08-15
        • 2012-10-17
        • 1970-01-01
        • 1970-01-01
        • 2013-06-06
        相关资源
        最近更新 更多