I want to test if $_GET
varriable is not empty before making wp query. I’ve tried this:
($miesto_name != '' ? "array('key' => 'miestas', 'value' => $miesto_name)," : '')
In the arrgs:
$args=array(
'post_type' => $type,
'posts_per_page' => 32,
'paged' => $paged,
'post_status' => 'publish',
'order' => 'ASC',
's' => $_GET['paieska'],
'meta_query' => array(
'relation' => 'AND',
($miesto_name != '' ? "array('key' => 'miestas', 'value' => $miesto_name)," : ''),
array(
'key' => 'imones_apskritis',
'value' => $apskritis_name
),
'city_clause' => array(
'key' => 'apmoketa'
)
),
'orderby' => array( 'city_clause' => 'DESC', 'title' => 'ASC' ),
'tax_query' => array(
array(
'taxonomy' => 'veiklos',
'field' => 'term_id',
'terms' => $_GET['veikla'])
)
);
But that didn’t worked, is there are any other way to check if value is not empty before doing queries?
I really can’t find a way to do this, and i don’t want to create separate $args for every condition…
This should do
You can simply make use of the
filter_input()
function in PHP which will do a few thingsCheck if the
$_GET
variable existsReturn the value if found, otherwise return false
Sanitize/Validate the value when a filter is set
You should note that you should NEVER EVER use non sanitized/non validated data from any
$_GET
variable (This also goes for any other input into a site like$_POST
and form fields). This is favorite spots that hackers use to inject malicious code into a site in order, so if you do not want your site hacked, ALWAYS ALWAYS SANTIZE/VALIDATE/ESCAPE any data coming into a site, and never trust anyone, not even yourselfYou can simply do something like the following if your
$_GET
value is expected to be a string (adjust the filter according to data type expected)