Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Having an issue using the Advanced Custom Fields Wordpress plugin. I've got 2 Custom Post Types

  1. pa_supplier
  2. pa_supplier_product

There is a one to many relationship between a supplier and their products. (One supplier has multiple products). I've got a page displaying a list of my suppliers and when I click on a supplier I am taken to single-pa_supplier page where I would like to display a list of the products associated with that Supplier.

I've successfully managed to make a query on this page and display products, but they are not the corresponding ones to the Supplier.

This works: - It displays a list of products with a quantity of 10,000 as expected

<?php $products = get_posts(array(
                'post_type' => 'pa_supplier_product',
                'posts_per_page' => '100',
                'meta_key' => 'quantity_per_pallet',
                'meta_value' => '10000',
                'compare' => 'LIKE'
            ));?>

This does not work: - I was hoping it would display all products that have a supplier called "Cavan Box"

<?php $products = get_posts(array(
                'post_type' => 'pa_supplier_product',
                'posts_per_page' => '100',
                'meta_query' => array(
                 array(
                   'key' => 'supplier',
                   'value' => 'Cavan Box',
                   'compare' => 'LIKE'
                  )
                )
            ));?>

I don't know why the second query is not working, I've a field called supplier and it is of type Relationship. when I create a product I assign it a supplier. Any help would be much appreciated.

share|improve this question
    
Is it the name that gets stored in that field? Seems more likely it would be the associated post ID. –  Milo Jun 5 at 15:49
    
No sorry, I was just hardcoding a value I knew would be in the supplier field. –  Javacadabra Jun 5 at 15:50

1 Answer 1

I'm pretty sure ACF stores relationship field values as serialized data, which could explain why your query is not working. From their documentation - http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/ (the last example) it looks like you should query by ID of the post which has the relationship, not the value of the custom field.

Note: I have had issues before when querying by name or title - strange behaviour with ordering, duplicate entries showing up etc - when I queried by ID instead I had much better and faster results.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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