Support

Account

Home Forums General Issues Filter Post By Query If the Value is Null

Solving

Filter Post By Query If the Value is Null

  • I need to filter the post querying though if value is Null

    Below is the code where i am passing the values dynamically by user input, so user can input all 4 fields or either any of three fields, or any of the two fields or any of the one field

    So i need to query the post accordingly though if any of the field is empty also

    $args = array(
        'numberposts' => -1,
        'post_type' => 'post',
        'meta_query' => array(
            'relation' => 'OR',
               array(
                'key' => 'regulation',
                'value' => $regulation,
                'compare' => '='
            ),
                array(
                'key' => 'subject_id',
                'value' => $subject_code,
                'compare' => '='
            ),
                array(
                'key' => 'department',
                'value' => $department,
                'compare' => '='
            ),
                array(
                'key' => 'categories',
                'value' => $categories,
                'compare' => '='
            ),
        )
    );

    For example: if the field input by the user is null then it should compare as not equal to, so how i can query if the value is not equal to “NULL” (NOT TO CHECK)

    i also tried with the following example but not working

    $args = array(
        'numberposts' => -1,
        'post_type' => 'post',
        'meta_query' => array(
            'relation' => 'OR',
               array(
                'key' => 'regulation',
                'value' => $regulation,
                'compare' => '='
            ),
                array(
                'key' => 'regulation',
                'value' => '',
                'compare' => '!='
            ),
                array(
                'key' => 'subject_id',
                'value' => $subject_code,
                'compare' => '='
            ),
                array(
                'key' => 'subject_id',
                'value' => '',
                'compare' => '!='
            ),
                array(
                'key' => 'department',
                'value' => $department,
                'compare' => '='
            ),
                 array(
                'key' => 'department',
                'value' => '',
                'compare' => '!='
            ),
                array(
                'key' => 'categories',
                'value' => $categories,
                'compare' => '='
            ),
                 array(
                'key' => 'categories',
                'value' => '',
                'compare' => '!='
            ),
        )
    );
    
  • checking for NULL value in meta_queries is:
    $args = array(
    ‘numberposts’ => -1,
    ‘post_type’ => ‘post’,
    ‘meta_query’ => array(
    ‘relation’ => ‘OR’,
    array(
    ‘key’ => ‘regulation’,
    ‘compare’ => ‘NOT EXISTS
    )
    )
    )

    it is key word for SQL IS NULL

  • wp-includes/meta.php LINE 765

    			if ( 'NOT EXISTS' == $meta_compare ) {
    				$join[$i]  = "LEFT JOIN $meta_table";
    				$join[$i] .= $i ? " AS $alias" : '';
    				$join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column AND $alias.meta_key = '$meta_key')";
    
    				$where[$k] = ' ' . $alias . '.' . $meta_id_column . ' IS NULL';
    
    				continue;
    			}
    
  • Here a good example for your question and answer for your problem : http://www.balitaeverywhere.com/how-meta-query-work-even-if-the-value-of-the-form-is-empty/ Dont forget to like and share the page .

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Filter Post By Query If the Value is Null’ is closed to new replies.