Support

Account

Home Forums General Issues Querying taxonomy field inside a repeater?

Solving

Querying taxonomy field inside a repeater?

    • rlocke

    • September 25, 2013 at 8:48 am

    Hello,

    I have a custom post type called “product”. Using ACF, I’ve created a repeater image field, with the following sub-fields:

    – image (image upload)
    – wood (checkbox -custom taxonomy named “woods”)
    – type (select – either a cabinet or door)

    I’m trying to create a query that returns all images, based on the wood (such as Cherry or Oak), regardless of the type.

    I looked at Example #5 here:

    http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/

    And it works with a select list, but I can’t get it to work on taxonomy (nothing is ever returned).

    Here’s my code snippet:

    
    function my_posts_where( $where )
    {
    	$where = str_replace("meta_key = 'image_%_wood'", "meta_key LIKE 'image_%_wood'", $where);
     
    	return $where;
    }
     
    add_filter('posts_where', 'my_posts_where');
     
    
    // args
    
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'product',
    	'meta_query' => array(
    		array(
    			'key' => 'image_%_wood',
    			'value' => 'Cherry'
    		)
    	)
    );
    

    Is it possible to query taxonomies within a repeater?

    I know I could change it to a select list, but I’d rather have it as taxonomy (if possible), which makes it easier for the client to update (and it’s also being used elsewhere).

    Thanks!

    • Elliot

    • September 25, 2013 at 1:03 pm

    Hi @rlocke

    I think the issue is that the value ‘Cherry’ is not correct. ACF saves the term ID as the value, not the term name.

    Changing this to the ID should work.

    You can check your wp_postmeta for clarification on the value that is actually saved!

    Thanks
    E

    • panmac

    • November 4, 2013 at 3:59 am

    rlocke, were you able to sort this out? I’m having exactly the same issue – I’ve even changed my custom field to be a select instead of the original taxonomy I was using, but still nothing is returned. When I had it set as taxonomy, the ID didn’t return anything either. If I comment out the meta_query array, the query returns posts. I’ve checked that all my names are correct, correct spelling, nothing is doing the trick. 🙁 Any ideas?

    
    // i've got the function code here as in example, only using 'projects_%_type'
    
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'artist',
    	'meta_query' => array(
    		array(
    			'key' => 'projects_%_type',
    			'value' => 'lifestyle'
    			)
    		)
    	);
    
    • Elliot

    • November 4, 2013 at 4:21 pm

    Hi @panmac

    Have you read over this article:
    http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/

    To query a sub field value, you need to add in a few more steps than the normal meta_query args.

    Thanks
    E

    • panmac

    • November 5, 2013 at 2:51 pm

    Hi Elliot – thanks for your answer – Yes, I had read over that tutorial, but since the code example was basically exactly what I was looking for, I was hoping it would work easily since I’m not advanced enough in PHP to know how to implement the tutorial and take into account my CPT – “by joining the wp_postmeta table to the wp_posts table on the post_id columns.” (and yes, I googled how to join tables in sql, etc, but everything just left me with a blank page so clearly I’m not doing it correctly).

    The other factor is that I realized I need the sub_field to be checkboxes and not a single select, so that adds another layer of complexity that I don’t think I understand.

    Not sure at all what I’m missing. Sorry for being such a noob.

    • Elliot

    • November 5, 2013 at 3:35 pm

    Hi @panmac

    To query a sub field value, especially if it is an array (checkbox) value, you are going to have to understand completely the SQL going on and be able to debug the code line by line to find any issues. If this is looking to be in the ‘too hard’ barsket, I highly encourage you to hire a freelancer to take a look at this part of your project.

    However, it is possible to write the code you are seeking. Once you have got the LIKE SQL working, you will need to change the line:

    AND meta_value = %s

    to:

    AND meta_value LIKE %s

    and that should allow you to search for values within the array!

    Thanks for being a good sport.

    Thanks
    E

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.