1

I want to get all the posts for which the post_name(slug) is not set/empty as well as a meta_key does not exist.

$args = array (
    'post_type'              => 'books',
    'name'                   => '',
    'post_status'            => array('publish, draft'),
    'posts_per_page'         => -1,
    'meta_query'             => array(
        array(
            'key'       => 'book_isbn_id',
            'value'     => 'dummy',
            'compare'   => 'NOT EXISTS',
            'type'      => 'NUMERIC',
        ),
    ),
);
$query = new WP_Query( $args );
echo $query->request;

It seems that 'name' = '' or 'name' = ' ' is completely ignored from what I see from echo $query->request.

Am I doing anything wrong?

2
  • You can run it excluding the meta_query to check whether it gives you any result or not. Then you can add the meta_query to further filter out the result. In the meta_query, you have mentioned a string value but the type is NUMERIC. Is it correct? Commented Feb 3, 2014 at 3:34
  • Yes I tried it without the meta_query and the results are same. And yes the meta_value is required and can be anything as there is a bug in the NOT EXISTS compare type. See here
    – Praz
    Commented Feb 3, 2014 at 4:22

2 Answers 2

0

You can try this:

$args = array (
    'post_type'              => 'books',
    'name'                   => '',
    'post_status'            => array('publish, draft'),
    'posts_per_page'         => -1,
    'meta_query'             => array(
        array(
            'key'       => 'book_isbn_id',
            'value'     => 'dummy',
            'compare'   => 'NOT EXISTS',
            'type'      => 'NUMERIC',
        ),
    ),
);

add_filter( 'posts_where', 'custom_posts_where' );
$query = new WP_Query( $args );
echo $query->request;

where

function custom_posts_where( $where )
{
    remove_filter( current_filter(), __FUNCTION__ );
    if( FALSE === strpos( $where, 'post_name' ) )
    {
        $where .= sprintf( " AND %s.post_name = '' ", $GLOBALS['wpdb']->posts );
    }
    return $where;
}

to search for empty post names.

0

Posts name will never be empty as it is automatically set when you set the title. or if title is left blank then your post_name will be your post id. So your query will never return any results unless you've hacked the wordpress core...

2
  • Not the answer I am looking for. In my case, I have custom posts posted programmatically with the post_name empty.
    – Praz
    Commented Feb 3, 2014 at 5:43
  • 1
    can you post the code in pastebin or somewhere else to be more specific to answer your questions
    – Raajen
    Commented Feb 3, 2014 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.