0

I've created a custom field 'entity_person_relevance' ranging from 1 to 3 in order to sort the results by relevance. The problem is that there are already more than 300 posts and the following query only shows posts with this custom field already set:

$query = ( array (
        'post_type'      => 'entity',
        'posts_per_page' => 20,
        'meta_query'     => array(
            array(
                'key'       => 'entity_person_profile_type',
                'value'     => $term->term_id,
                'compare'   => '='
            )
        ),
        'orderby'  => 'meta_value_num',           
        'meta_key' => 'entity_person_relevance', 
        'order'    => 'DESC',
        'paged'    => $paged
        )
);

I'd like to show also the posts which doesn't have the custom field already set.

0

You can add the custom field to all of the post:

$posts = get_posts(array('post_type'=>'entity', 'posts_per_page'=>-1);

foreach($posts as $post){
    if(get_post_meta($post->ID, 'entity_person_relevance', true) == null ){
        $term = wp_get_object_terms( $post->ID, 'yourtermcat'); // see codex to $wp_error handling or just set $term to 1, 2 or 3 

        update_post_meta($post->ID, 'entity_person_relevance', $term->term_id); 
    }

}
0

Change the meta_query part of your code to something like this (untested):

'meta_query'     => array(
        'relation' => 'OR',
        array(
            'key'     => 'entity_person_profile_type',
            'value'   => $term->term_id,
            'compare' => '='
        ),
        array(
            'key'     => 'entity_person_profile_type',
            'compare' => 'NOT EXISTS'
        ),
    ),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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