• Resolved PIW

    (@dbaldan)


    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.

    [No bumping. If it’s that urgent, consider hiring someone.]

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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

    (@dbaldan)

    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.