Title: Query_posts and custom fields
Last modified: August 20, 2016

---

# Query_posts and custom fields

 *  Resolved [PIW](https://wordpress.org/support/users/dbaldan/)
 * (@dbaldan)
 * [14 years ago](https://wordpress.org/support/topic/query_posts-and-custom-fields/)
 * Hello guys.
 * I have some posts with a custom field called ‘destaque’ with the value ‘sim’,
   which works kinda like a sticky post, but in a differnt way.
 * I am retrieving those posts by using this code:
 * `query_posts( array( 'cat' => '', 'posts_per_page' =>2, 'paged' => get_query_var('
   paged'),'meta_key' => 'destaque', 'meta_value' => 'sim') );`
 * And it works.
 * But, I have a second loop and, on that loop, _I must retrieve JUST the posts 
   which DOENS’T have a custom field called ‘destaque’_.
 * **How can I do this?** I was looking those docs [ but I can’t find the right option.](http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters)
 * _[[No bumping](http://codex.wordpress.org/Forum_Welcome#No_Bumping). If it’s 
   that urgent, consider [hiring someone](http://jobs.wordpress.net/).]_

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years ago](https://wordpress.org/support/topic/query_posts-and-custom-fields/#post-2784823)
 * You can do that by using a query to get the post IDs of all posts that have the
   custom field and then excluding them from the main query:
 *     ```
       $ids = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'destaque' ");
       query_posts( array( 'cat' => '', 'posts_per_page' =>2, 'paged' => get_query_var('paged'),'post__not_in' => $ids ) );
       ```
   
 *  Thread Starter [PIW](https://wordpress.org/support/users/dbaldan/)
 * (@dbaldan)
 * [14 years ago](https://wordpress.org/support/topic/query_posts-and-custom-fields/#post-2784824)
 * Works like a charm!
 * I was trying to use this, but without success:
 *     ```
       $args = array(
       	'cat' => '',
       	'posts_per_page' =>2,
       	'paged' => get_query_var('paged'),
       	'meta_query' => array(
       		array(
       			'key' => 'destaque',
       			'value' => '',
       			'compare' => 'LIKE'
       		)
       	)
        );
       $query = new WP_Query( $args );
       ```
   
 * God bless you 🙂

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

The topic ‘Query_posts and custom fields’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [meta_key](https://wordpress.org/support/topic-tag/meta_key/)
 * [meta_value)](https://wordpress.org/support/topic-tag/meta_value/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [PIW](https://wordpress.org/support/users/dbaldan/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/query_posts-and-custom-fields/#post-2784824)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
