Checking if variable is not empty before WP_query

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:

Read More
$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…

Related posts

2 comments

  1. This should do

    $paieska = isset( $_GET['paieska'] ) ? $_GET['paieska'] : 'Value if Get is Empty';
    $veikla = isset( $_GET['veikla'] ) ? $_GET['veikla'] : 'Value if Get is Empty';
    $custom_array = !empty( $miesto_name ) ? array('key' => 'miestas', 'value' => $miesto_name) : array();
    $args=array(
        'post_type' => $type,
        'posts_per_page' => 32,
        'paged' => $paged,
        'post_status' => 'publish',
        'order'   => 'ASC',
        's' => $paieska,
        'meta_query' => array(
            'relation'      => 'AND',
            $custom_array,
            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' => $veikla
        )
    );
    
  2. You can simply make use of the filter_input() function in PHP which will do a few things

    • Check if the $_GET variable exists

    • Return 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 yourself

    You 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)

    $value = filter_input( INPUT_GET, 'name_of_key', FILTER_SANITIZE_STRING );
    var_dump( $value );
    

Comments are closed.