Support

Account

Home Forums Backend Issues (wp-admin) How can I set true/false field value to always be false instead of NULL Reply To: How can I set true/false field value to always be false instead of NULL

  • There is no work-a-round. I was just doing some testing to see what happens and you just can’t order posts by a field that does not exist on every post.

    What you have to do is to create this field with a default value on every “product”

    
    add_filter('init', 'update_all_product_acf_field');
    // query to get all posts that are missing the meta value
    function update_all_product_acf_field() {
      $args =array(
        'post_type' => 'your-post_type', // change to correct post type
        'posts_per_page' => -1, // all mosts
        'meta_query' => array(
          array(
            // missing this field
            'key' => 'show_on_first_page',
            'compare' => 'NOT EXISTS',
          ),
        ),
        'fields' => 'ids' // all we need is the id
      );
      $query = new wp_query($args);
      if (!empty($query->posts)) {
        foreach ($query->posts as $post_id) {
          // use field key for your field
          // because it does not exist
          update_field('field_XXXXXXX', 0, $post_id);
        }
      }
    }
    

    More then likely the above will time out the loading of your site one or several times. When the site is able to load you can remove the init action because all the posts will be updated…. (it can’t find any more posts with the missing meta key)