4

I have a custom content type with a postal_address field provided by the module addressfield. postal_address fields are one field in the custom content type. This one field is comprised of many subfields like: country, name_line, and postal_code.

How can the core function EntityFieldQuery be used to retrieve nodes of my custom content type based on their postal code value (a subfield of the postal_address field)?

Specifically, I do not know what to put in the code below instead of the text: what_goes_here:

$query  =  new EntityFieldQuery();

$entities  =  $query->entityCondition( 'entity_type', 'node' )
  ->propertyCondition( 'status', 1)
  ->propertyCondition( 'type', 'consumer_store' )
  ->fieldCondition( 'what_goes_here', 'value', $postal_codes )
  ->execute();
4
  • There's nothing special about an addressfield, you can use an EntityFieldQuery as you would with any other field type. Where are you stuck exactly? Could you post the code you've tried with results/error messages etc?
    – Clive
    Aug 18, 2014 at 17:44
  • I'm not sure it's that easy, because the value of of a postal_address field is an array of the subfields.
    – Jay Haase
    Aug 18, 2014 at 20:38
  • @JayHaase Do you allow addresses from multiple/any country?
    – mpdonadio
    Aug 18, 2014 at 20:41
  • @MPD: only from one country.
    – Jay Haase
    Aug 18, 2014 at 20:54

1 Answer 1

5

Although Addressfield can be extended, the standard columns like zip code are normal field columns and live in field storage (see addressfield_field_schema()).

In your case you want the postal_code column:

$query  =  new EntityFieldQuery();

$entities  =  $query->entityCondition( 'entity_type', 'node' )
  ->propertyCondition( 'status', 1)
  ->propertyCondition( 'type', 'consumer_store' )
  ->fieldCondition( 'field_postal_address', 'postal_code', $postal_codes )
  ->execute();
1
  • That works perfectly form me. Thank you for your patience!
    – Jay Haase
    Aug 18, 2014 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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