0

I've created a Field Group called Brands, in that field group I've added a field with a field name of 'select_brands' of type Checkbox (to be able to select multiple). In the choices I've added this:

bolou : Bolou
c6 : C6
deakin francis : Deakin Francis
fope : FOPE
piero milano : Piero Milano

From this I generate a drop down on the front page, along a second drop down generated from categories (the built in widget in the admin interface).

So, now I can pick – from two different drop downs – a combination of both Brand and Category. But, – I've now tried for three days to get the posts from the DB without any luck (yes, luck is actually what I'm counting on now, 'cause nothing seem to make sense).

Now, from the 3.456.745 different versions of an $args array I've tried, none work. Now, I need some help understanding what parameters I actually need to put where, as I simply can't grasp why this is so hard to get going.

I would expect the following to get me; posts of custom post type 'jewellery' with a category of whatever is selected in the category drop down ($category is an integer from the option value in the drop down) that is also of a specific Brand ($brand is a string from the above options from the custom field).

$args_jewellery = array(
    'post_type' => 'jewellery',
    'numberposts'   => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'terms' => array( $category )
        )
    ),
    'meta_query' => array(
        array(
            'key' => 'select_brands', <-- IS THIS RIGHT?
            'value' => $brand <-- AND IS THIS?
        )
    )
);

How do the fields below relate to my custom post type and its value?

I've even been trying with hard coded values that I KNOW exist, but still get nothing.

'meta_query' => array(
    array(
        'key' => 'select_brands', <-- ?
        'value' => $brand <-- ?
    )
)

And WHY isn't this working? What is it that I'm totally missing here ... ?

  • Why not also make brand a taxonomy, like category? – Milo Jun 8 '17 at 21:10
  • This seem counter intuitive from a user perspective. I have clients to consider, and even when the concept of taxonomy may seem easy to understand for developers, I see clients struggling with it. A checkbox that is checked they get ... and I never really understood why the interface for taxonomies are so different from Categories (I know of the hierarchical difference, but), to a client they seem the same. – blaasvaer Jun 8 '17 at 22:00
  • You can use an ACF field with a taxonomy if you want the same interface. The difference is that taxonomy queries are more efficient than meta queries. – Milo Jun 8 '17 at 22:06
0

Give this one a try:

$args_jewellery = array(
    'post_type' => 'jewellery',
    'numberposts'   => -1,
    'meta_query' => array(
        array(
            'key' => 'select_brands',
            'value' => '"' . $brand . '"',
            'compare' => 'LIKE'
        )
    )
);

And here's a link to the WP Query custom field parameters documentation page.

  • Thanks man, you put and end to three (3!) days of misery. Now, why is that nowhere to be found!? – blaasvaer Jun 8 '17 at 21:55
  • I know this feeling, sometimes things are hard to find - I've added a link to the documentation in my answer above, so you can check all available options if you need something else in the future :) – Picard Jun 8 '17 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.