Title: Query using string from custom field as array value
Last modified: August 24, 2016

---

# Query using string from custom field as array value

 *  [pdg87](https://wordpress.org/support/users/pdg87/)
 * (@pdg87)
 * [11 years ago](https://wordpress.org/support/topic/query-using-string-from-custom-field-as-array-value/)
 * Firstly, what I am trying to do is display posts from Category x on posts from
   Category y where text from a custom field on post from Cat y matches text from
   one of 2 custom fields on cat x.
 * I have a custom text field on each post from cat x and y to store a list of attributes.
   e.g. red,car,sports,alloys.
 * I then want to check to see if they match up between posts and pull in the post
   where they do.
 * Firstly, is there a much better way of doing this?
 * I am currently trying WP_Query. My query is like this:
 *     ```
       $args = array(
       	'meta_query' => array(
       		'relation' => 'OR',
       		array(
       			'key'     => 'style_atrributes',
       			'value'   =>  array('main_atrributes'),
       			'compare' => 'LIKE'
       		),
       		array(
       			'key'     => 'car_atributes',
       			'value'   => array('main_atrributes'),
       			'compare' => 'LIKE'
       		),
       	),
       );
       ```
   
 * I have placed this in the post template for category y. As above it just displays
   all posts from cat x. If I tweak it and change the array to (‘red’, ‘car’) it
   works and pulls in the correct posts.
 * Is it possible to use text as part of an array? I have tried storing the text
   as “red”, “car” and that didnt work either. If I print the field out it displays
   as above.
 * Anyone point me in the right direction?

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

 *  Thread Starter [pdg87](https://wordpress.org/support/users/pdg87/)
 * (@pdg87)
 * [11 years ago](https://wordpress.org/support/topic/query-using-string-from-custom-field-as-array-value/#post-6132533)
 * When I call ‘main_atrributes’ as a value, will in get the string from every single
   custom field with this name, rather than for just the page you are on?
 * This could be where I’m going wrong.. if so how do I use the custom field only
   from current post you are viewing?
 *  Thread Starter [pdg87](https://wordpress.org/support/users/pdg87/)
 * (@pdg87)
 * [11 years ago](https://wordpress.org/support/topic/query-using-string-from-custom-field-as-array-value/#post-6132539)
 * Sorry Im talking to myself 😛 But do I need to add this somehow to use the current
   post custom field value?
 * $value = get_field( “main_atrributes” );
 * If so how would I work it into the array?

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

The topic ‘Query using string from custom field as array value’ is closed to new
replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 1 participant
 * Last reply from: [pdg87](https://wordpress.org/support/users/pdg87/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/query-using-string-from-custom-field-as-array-value/#post-6132539)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
