Support

Account

Home Forums General Issues Change Post Archive Image with Filters

Helping

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.

    null

    My Repeater Field looks a little like this:

      Field Name: images / Field Type: repeater
    • Field Name: product_image / Field Type: image
    • Field Name: product_colour / Field Type: select

    <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

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

The topic ‘Change Post Archive Image with Filters’ is closed to new replies.