Home › Forums › General Issues › Change Post Archive Image with Filters
Okay, so I’m really trying to squeeze every inch out of this unbelievable plugin, and I have a question.
I’ve got post type set up (product). Each (product) has one Featured Image and a Repeater Field (images) attached to it. This Repeater Field holds all my product images and also its various colours.
My Repeater Field looks a little like this:
<br>
I’m trying to work out how on the archive-product.php
page to display all my products alongside their Featured Image. Which is pretty easy, as it turns out.
The bit I’m having trouble with is working out how to let the customer click a colour and filter those products by that colour and changing the image from the Featured Image to the product image that relates to that colour.
Any help with this is appreciated. Thanks in advance!
Hi @realph
Thanks for the question, it is very clear to see what you are attempting to do.
The above will not be easy, but it is not impossible. Lets start with filtering the products that are ‘blue’.
You can use this tutorial here to query sub field values:
http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/
This tutorial may seem a bit complex, but your task here is to find all the $rows in the postmeta table, where ‘blue’ was selected.
Withing the $rows
loop, you can find the post_id like so $post_id = $row->post_id
;
This allows to get the product ID that contains this ‘blue’ color. Instead of assigning this to a variable called $post_id
, you should append it to an array like so: $ids[] = $row->post_id
;
After your loop is complete, you should have a populated $ids array which you can now use in a WP_Query argument in the ‘posts__in’ arg!
To show the blue image as the featured one, just loop through all the repeater rows and then match the ‘product_colour’ value to the current queried color. When a match is found, display that image!
Thanks
E
The topic ‘Change Post Archive Image with Filters’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.