0

我正在使用插件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
                                )                                                           
                        )       
                        ));
4

2 回答 2

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

?>
于 2014-01-30T11:33:49.420 回答
0

我实际上是通过使用 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(); 
于 2014-01-30T11:49:17.083 回答