Support

Account

Home Forums General Issues Select posts by Category && Custom Field?

Solved

Select posts by Category && Custom Field?

  • I’ve got a range of products organized using the built in Categories, and an ACF of type Brand. I’ve created two drop downs; one with Categories and another with Brands.

    Now, how do I get posts that are of both a category type and a specific brand?

    I’ve been googling the crap out of this, but can’t seem to find an answer to this fairly simple task.

  • What do you mean by “an ACF of type Brand”? What type of field is this?

  • It’s a select.

    What I would ideally like is a copy of the Category widget (just with Brands instead), but I can’t seem to get my head around it. So therefore I’m trying to use a custom field instead, but I’m having serious trouble figuring out how to retrieve post using multiple ‘parameters’.

    I’ve been trying something in this direction:

    
    $args_jewellery = array(
    	'post_type' => 'jewellery',
    	'category' => $category,
    	'numberposts' => -1,
    	'meta_query' => array(
    		array(
    			'key' => $brand
    			// 'compare' => 'EXISTS'
    			)
    	)
    );
  • I was going to answer last night but then I thought the way you worded it that it might be another taxonomy.

    Anyway, you just need to add both a tax query and a meta query

    
    $args = array(
      'post_type' => 'product', // or whatever
      'tax_query' => array(
        array(
          'taxonomy' => 'category',
          'terms' => array($term_id) // must be an array of term IDs
        )
      ),
      'meta_query' => array(
        array(
          'key' => 'brand',
          'value' => 'some brand'
        )
      )
    )
    

    https://codex.wordpress.org/Class_Reference/WP_Query

  • Thanks, John, but I have to adapt this to my code to fully understand it:

    I have a range of jewellery (using a Custom Post Type = jewellery) that I separate by Category (using the built in Categories of WP) and Brands (using a custom field to select the brand in the custom post type).

    From that I create two separate drop downs: Category with values of IDs of categories, and Brands with values of strings.

    So if the user select a Brand from one of the selects and a Category from the other select, the code you provide should look like this, right:

    'post_type' => 'jewellery',
    'tax_query' => array(
    		array(
    			'taxonomy' => 'category',
    			'terms' => array( $category ) // <-- THIS I GET FROM $_GET['c']
    		)
    	),
    	'meta_query' => array(
    	array(
    		'key' => 'select_brand', // <-- WHAT DO I PUT HERE (THE CUSTOM FIELD VALUE, this is what I named it.)
    		'value' => $brand // <-- THIS I GET FROM $_GET['b']
    	)
    )
    
  • OK, day three of trying to figure this one out.

    Is this simply not possible to do in WP with custom fields!?

  • I’m using trial and error with values I know exists:

    But no matter what I put it, I still get nothing:

    $args_jewellery = array(
    	'post_type' => 'jewellery',
    	'numberposts'	=> -1,
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category',
    			'terms' => array( 6 ) // Also tried category name as string 'Ringe', no difference
    		)
    	),
    	'meta_query' => array(
    		array(
    			'key' => 'select_brands',
    			'value' => 'bolou'
    		)
    	)
    );
    

    If I remove the meta_query part I get all rings. But why isn’t the meta_query part working – these are values I know exists. But I don’t know if that’s what I’m supposed to use!?

    ANY help is appreciated!

  • 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?

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

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

  • So, a guy on wordpress.stackexchange.com gave me this hint:

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

    Now, why would it take me three days to wrap that shit in quotation marks and use LIKE?

    Sigh!

  • Sorry I didn’t get back to you before you worked it out. I did get a notice of your reply but I just couldn’t catch a break to reply. I had a feeling that it would be the solution you came up with. A select field can store values as an array.

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Select posts by Category && Custom Field?’ is closed to new replies.