mardi 20 décembre 2016

Check if Querystrings are not visible on url

I have this code to check if query strings are present and echo contents based on the values.

The code is working if:

http://ift.tt/2hYpzjd
http://ift.tt/2hTlKi5
http://ift.tt/2hYsA30
http://ift.tt/2hTp7Wa
http://ift.tt/2hYuhxs
.
.
and so forth.

but if the url is just www.mywebsite.com without querystring the parent else block is not executed.

My current code is:

<?php 
// args
    if ($_GET){
        if ($_GET['category'] && $_GET['country']) {
            $args = array(
            'numberposts'   => -1,
            'post_type'     => 'job_order',
            'meta_query'    => array(
                'relation'      => 'AND',
                array(
                    'key'       => 'j_category',
                    'value'     => $_GET['category'],
                    'compare'   => '='
                ),
                array(
                    'key'       => 'j_country',
                    'value'     => $_GET['country'],
                    'compare'   => '='
                    )
                )
            );
        } elseif ($_GET['category']) {
            $args = array(
                'numberposts'   => -1,
                'post_type'     => 'job_order',
                'meta_key'      => 'j_category',
                'meta_value'    => $_GET['category']
                );
        } elseif ($_GET['country']) {
            $args = array(
                'numberposts'   => -1,
                'post_type'     => 'job_order',
                'meta_key'      => 'j_country',
                'meta_value'    => $_GET['country']
            );
        } else {
            $args = array(
                'numberposts'   => -1,
                'post_type'     => 'job_order'
                );
        }
    } else{
        $args = array(
            'numberposts'   => -1,
            'post_type'     => 'job_order'
            );
    }
    // query
    $the_query = new WP_Query( $args );
    if( $the_query->have_posts() ): 
    while( $the_query->have_posts() ) : $the_query->the_post(); 
?>

Aucun commentaire:

Enregistrer un commentaire