Title: WordPress Custom Post Type Query
Last modified: August 21, 2016

---

# WordPress Custom Post Type Query

 *  [Manan](https://wordpress.org/support/users/mananpatel/)
 * (@mananpatel)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/wordpress-custom-post-type-query/)
 * I have this query for custom filed values (array based values):
 *     ```
       <?php
           // args
           $args = array(
               'numberposts' => -1,
               'post_type' => 'villa',
               'meta_query' => array(
               'relation' => 'AND',
                   array(
                           'key' => 'amenities',
                           'value' => 'Internet',
                           'compare' => 'LIKE'
                       )
                   )
               );
           // get results
           $the_query = new WP_Query( $args );
   
           // The Loop
       ?>
       <?php if( $the_query->have_posts() ): ?>
           <ul>
           <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
               <li>
                   <?php echo the_title(); ?>
               </li>
           <?php endwhile; ?>
           </ul>
       <?php endif; ?>
       ```
   
 * Now I want to generate bellow part dynamically:
 *     ```
       array(
                   'key' => 'amenities',
                   'value' => 'Internet',
                   'compare' => 'LIKE'
               )
           )
       ```
   
 * Bellow is my implemented code but now working:
 *     ```
       $amenities_exp = explode(",", $villa_amenities);
           $aCount = 0;
   
           $args = 'array(
                 \'numberposts\' => -1,
                 \'post_type\' => \'villa\',
                 \'meta_query\' => array(
                 \'relation\' => \'OR\',';
   
           for($a=0; $a<count($amenities_exp); $a++){
               "array(
                       'key' => 'amenities',
                       'value' => '".$amenities_exp[$a]."',
                       'compare' => 'LIKE'
                   )";
               if($aCount != count($amenities_exp) - 1){
                   echo ",";
               }else{
                   echo "";
               }
               $aCount++;
           }
   
           "));";
       ```
   

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/wordpress-custom-post-type-query/#post-4574128)
 * You’re trying to build a complex string that looks like an array definition, 
   but you need an actual array, not a string that looks like one. You can build
   up a meta_query array in a loop, then pass the final result as part of the whole
   WP_Query arguments array. Something like this, with the specifics taken out to
   better demonstrate the concept:
 *     ```
       $meta_args = array('relation' => 'AND');
       foreach $amenities_exp as $amenity {
          $meta_args[] = array( 'value' => $amenity,);
       }
       $args = array('meta_query' => $meta_args);
       ```
   
 * Flesh out the array definitions of this structure and it should work for you.

Viewing 1 replies (of 1 total)

The topic ‘WordPress Custom Post Type Query’ is closed to new replies.

## Tags

 * [advancedcustomfields](https://wordpress.org/support/topic-tag/advancedcustomfields/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [12 years, 3 months ago](https://wordpress.org/support/topic/wordpress-custom-post-type-query/#post-4574128)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
