简体   繁体   中英

I have query to retrieve all posts in wordpress but how can i check the condition for post meta_key =“develop” and meta_value='anything'

SELECT DISTINCT date(p.post_date) as post_date
FROM $wpdb->posts p
LEFT JOIN $wpdb->postmeta pm ON p.ID = pm.post_id
WHERE p.post_status='publish'
    AND p.post_type IN ('%s')
ORDER BY p.post_date DESC

Here it reads all dates of the posts. My problem is that query should exclude the posts which has meta_value(it can be anything) of which meta_key="develop" . How can i write the query for that ?

I feel it will be in this form for wp_query

$args[ 'meta_query' ]= array (

        'relation'= >' AND ',

                array (
                        'key' = > 'respect' 
                ),
                array (
                        'key' => 'develop',
                        'compare' => 'NOT EXISTS'

                )
            ); 
   $query = new Wp_query($args);

So you're trying to get all posts that don't have a meta key equal to "develop"?

You could do this:

$query = new WP_Query( array( 
    'meta_key' => 'develop',
    'meta_compare' => 'NOT EXISTS' 
) );

Also, see this note from the Codex if you're using a version of Wordpress less than 3.9:

"(Note: Due to bug #23268, value is required for NOT EXISTS comparisons to work correctly prior to 3.9. You must supply some string for the value parameter. An empty string or NULL will NOT work. However, any other string will do the trick and will NOT show up in your SQL when using NOT EXISTS. Need inspiration? How about 'bug #23268'.)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM