Question

I'm using the plugin http://www.advancedcustomfields.com and trying to query thousands of custom posts.

I have the loop below which is checking whether a post doesn't have the custom field sticky posts selected as yes.

In order for this to work I would have to manually go through thousands of posts and save them so the custom field value is saved.

How can I add to this query to check if there is a value for the custom field at all?

$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
                                )                                                           
                        )       
                        ));
Was it helpful?

Solution 2

I actually solved this by using the update function in ACF - so I updated all posts to have a custom field value had the very least, thus cutting out the need to query a blank custom field.

$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(); 

OTHER TIPS

Pleas try this code to get all the custom fields of post

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

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top