-3

I am running the following in my functions.php file:

$query = new WP_Query(array(
    'post_type' => 'gear',
    'orderby' => 'title',
    'order' => 'ASC',
    'author' => $current_user->ID,
    'posts_per_page' => -1,
    'meta_key'          => 'brand_preselect',
    'orderby'           => 'meta_value',
    'meta_key'          => 'category_tax',
    'orderby'           => 'meta_value',
    ));
    

        foreach($query->posts as $product_id=>$macthed_product){
            $choices[$macthed_product->ID] = '<span>' . $macthed_product->brand_preselect . '</span>' . $macthed_product->post_title;
}

This all works pretty well, but the problem is $macthed_product->brand_preselect is returning the value of the checkbox and not the Label. On this field (brand_preselect) I have it only set to return the Label though. How do I get it to return the Label and not the value?

Paul VI
  • 515
  • 1
  • 8
  • 25
  • Have you changed you field settings after creating the data? Fieldsettings will not update past set values. – Code Spirit Aug 06 '20 at 09:09
  • Not sure what you mean? I am basically using the solution posted at: https://stackoverflow.com/questions/21965679/add-posts-select-checkboxes-using-advanced-custom-fields – Paul VI Aug 06 '20 at 09:10
  • 1
    _“On this field (brand_preselect) I have it only set to return the Label though.”_ - that applies only when you use ACF methods to get the field value; here however, you are directly querying the database yourself. – CBroe Aug 06 '20 at 09:10
  • @CBroe OK, so is there a way to grab the Label instead of the value? – Paul VI Aug 06 '20 at 09:11
  • https://www.advancedcustomfields.com/resources/get_field/ – CBroe Aug 06 '20 at 09:12
  • @CBroe does not help me in this case – Paul VI Aug 06 '20 at 09:19
  • 1
    _Why_ not? Explain stuff like this, instead of just stating it, as if it was an actual fact. – CBroe Aug 06 '20 at 09:20
  • @CBroe I don't know why the get_field doesn't return anything - so if I use a get_field on the brand_preselect it returns nothing – Paul VI Aug 06 '20 at 09:22
  • Then _show us_ how you tried it. – CBroe Aug 06 '20 at 09:23
  • @CBroe So within that foreach loop - $zz = get_field('brand_preselect'); echo $zz; Returns nothing – Paul VI Aug 06 '20 at 09:29
  • 1
    You need to pass in the post ID as second parameter. You have no “current post” here, that is only a thing when you work within the actual WP loops. – CBroe Aug 06 '20 at 09:34
  • @CBroe - again nothing. Have a global $post and then $zz = get_field('brand_preselect',$post->ID); echo $zz; – Paul VI Aug 06 '20 at 09:37
  • Should be `$product_id` inside your loop, no? Resp. `$macthed_product->ID` – CBroe Aug 06 '20 at 09:37
  • @CBroe - great, so that works but now how to I order by $zz = get_field('brand_preselect',$macthed_product->ID); – Paul VI Aug 06 '20 at 09:40
  • Uh, that will not be possible directly. In the post_meta table, _only_ the field value is stored. Your label is stored as part of the ACF field configuration. If you don’t need to paginate this, but just output all the posts in one go, then sorting the array afterwards in PHP is probably your best option here. (`usort` with a little custom comparison function, https://stackoverflow.com/q/17364127/1427878) – CBroe Aug 06 '20 at 09:44

1 Answers1

0

Solved via CBroe - $zz = get_field('brand_preselect',$macthed_product->ID);

Paul VI
  • 515
  • 1
  • 8
  • 25