Home › Forums › General Issues › 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 .
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.