Search code examples
phpwordpressfiltercustom-post-typetaxonomy

Remove URL query parameter if no value exists


I found this bit of very useful code for generating a dropdown to use as a URL query string for multiple custom taxonomies.

The code below, paired with a bunch of if(isset($_GET['taxonomies'])) and if(empty($_GET['taxonomies'])) declarations in PHP in the archive.php file, sort of works in filtering posts across three custom taxonomies.

However, it only works if URL query parameters are not empty. For instance:

example.com/custom-post-type-slug/?taxonomy1=term1&taxonomy2=termA&taxonomy3=termZ

... works just fine.

example.com/custom-post-type-slug/?taxonomy1=term1&taxonomy2=termA

... also works fine.

However, the dropdown created by the code below will generate a URL query string that includes empty query parameters such as:

example.com/custom-post-type-slug/?taxonomy1=term1&taxonomy2=&taxonomy3=termZ

The empty query parameter for taxonomy 2 breaks it. As does all query parameters missing:

example.com/custom-post-type-slug/?taxonomy1=&taxonomy2=&taxonomy3=

How do I tweak the code below to remove the URL query parameter if there's no value?

The most ideal situation is that if there's no URL query value, the query parameter is removed from the string. How do I do that with this code?

This is in my functions.php file.

function get_terms_dropdown_taxonomy1($taxonomies, $args){
            $myterms = get_terms($taxonomies, $args);
            $output ="<select name='taxonomy1'>";
            $output .="<option value=''>All Taxonomy 1</option>";
            foreach($myterms as $term){
                    $root_url = get_bloginfo('url');
                    $term_taxonomy=$term->taxonomy;
                    $term_slug=$term->slug;
                    $term_name =$term->name;
                    $link = $term_slug;
                    $output .="<option value='".$link."'>".$term_name."</option>";
            }
            $output .="</select>";
    return $output;
    }

function get_terms_dropdown_taxonomy2($taxonomies, $args){
            $myterms = get_terms($taxonomies, $args);
            $output ="<select name='taxonomy2'>";
            $output .="<option value=''>All Taxonomy 2</option>";
            foreach($myterms as $term){
                    $root_url = get_bloginfo('url');
                    $term_taxonomy=$term->taxonomy;
                    $term_slug=$term->slug;
                    $term_name =$term->name;
                    $link = $term_slug;
                    $output .="<option value='".$link."'>".$term_name."</option>";
            }
            $output .="</select>";
    return $output;
    }

function get_terms_dropdown_taxonomy3($taxonomies, $args){
            $myterms = get_terms($taxonomies, $args);
            $output ="<select name='taxonomy3'>";
            $output .="<option value=''>All Taxonomy 3</option>";
            foreach($myterms as $term){
                    $root_url = get_bloginfo('url');
                    $term_taxonomy=$term->taxonomy;
                    $term_slug=$term->slug;
                    $term_name =$term->name;
                    $link = $term_slug;
                    $output .="<option value='".$link."'>".$term_name."</option>";
            }
            $output .="</select>";
    return $output;
    }

Meanwhile, this is output within a template file.

        <?php
            $taxonomies = array('taxonomy1');
            $args = array('orderby'=>'name','hide_empty'=>1);
            $select = get_terms_dropdown_taxonomy1($taxonomies, $args);
            $select = preg_replace("#<select([^>]*)>#", "<select$1 id='taxonomy1' class='dropdown' aria-label='Select a taxonomy 1'>", $select);
            echo $select;
        ?>

        <?php
            $taxonomies = array('taxonomy2');
            $args = array('orderby'=>'name','hide_empty'=>1);
            $select = get_terms_dropdown_taxonomy2($taxonomies, $args);
            $select = preg_replace("#<select([^>]*)>#", "<select$1 id='taxonomy2' class='dropdown' aria-label='Select a taxonomy 2'>", $select);
            echo $select;
        ?>

        <?php
            $taxonomies = array('taxonomy3');
            $args = array('orderby'=>'name','hide_empty'=>1);
            $select = get_terms_dropdown_taxonomy3($taxonomies, $args);
            $select = preg_replace("#<select([^>]*)>#", "<select$1 id='taxonomy3' class='dropdown' aria-label='Select a taxonomy 3'>", $select);
            echo $select;
        ?>

EDIT: Thanks to your help here is the final working code.

<?php
$url = parse_url($_SERVER['REQUEST_URI']);

    // WORKING | all taxonomy1, taxonomy2, and taxonomy3; no values for any taxonomy; displays six most recent posts

    if(empty($_GET['taxonomy1']) && empty($_GET['taxonomy2']) && empty($_GET['taxonomy3'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
    ) );
    }

    // WORKING | taxonomy1 only; no values for taxonomy2 or taxonomy3

    elseif(empty($_GET['taxonomy2']) && empty($_GET['taxonomy3'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => 'taxonomy1',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy1'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }

    // WORKING | issue only; no values for taxonomy1 or taxonomy3; displays six most recent taxonomy2 posts

    elseif(empty($_GET['taxonomy1']) && empty($_GET['taxonomy3'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => 'taxonomy2',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy2'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }

    // WORKING | taxonomy3 only; no values for taxonomy1 or taxonomy2

    elseif(empty($_GET['taxonomy1']) && empty($_GET['taxonomy2'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => 'taxonomy3',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy3'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }   

    // WORKING | taxonomy2 in taxonomy3; no values for taxonomy1

    elseif(empty($_GET['taxonomy1'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        'relation'      => 'AND',
                        array(
                            'taxonomy'  => 'taxonomy2',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy2'],
                            'operator'  => 'IN',
                        ),
                        array(
                            'taxonomy'  => 'taxonomy3',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy3'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }

    // WORKING | taxonomy1 in taxonomy3; no values for taxonomy2

    elseif(empty($_GET['taxonomy2'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        'relation'      => 'AND',
                        array(
                            'taxonomy'  => 'taxonomy1',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy1'],
                            'operator'  => 'IN',
                        ),
                        array(
                            'taxonomy'  => 'taxonomy3',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy3'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }

    // WORKING | taxonomy1 on taxonomy2; no values for taxonomy3

    elseif(empty($_GET['taxonomy3'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        'relation'      => 'AND',
                        array(
                            'taxonomy'  => 'taxonomy1',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy1'],
                            'operator'  => 'IN',
                        ),
                        array(
                            'taxonomy'  => 'taxonomy2',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy2'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }

elseif(isset($_GET['taxonomy1']) && ($_GET['taxonomy2']) && ($_GET['taxonomy3'])) {

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
    'post_type'         => 'custom_post_type',
    'publish_status'    => 'publish',
    'posts_per_page'    => 6,
    'paged'             => $paged,
                'tax_query'         => array(
                    'relation'      => 'AND',
                    array(
                        'taxonomy'  => 'taxonomy1',
                        'field'     => 'slug',
                        'terms'     => $_GET['taxonomy1'],
                        'operator'  => 'IN',
                    ),
                    array(
                        'taxonomy'  => 'taxonomy2',
                        'field'     => 'slug',
                        'terms'     => $_GET['taxonomy2'],
                        'operator'  => 'IN',
                    ),
                    array(
                        'taxonomy'  => 'taxonomy3',
                        'field'     => 'slug',
                        'terms'     => $_GET['taxonomy3'],
                        'operator'  => 'IN',
                    ),
                ),
) );
}

    else{

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
    ) );
    }

    if ( $exec_query->have_posts() ) { ?><?php while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>

Solution

  • isset() will return true if your query parameter exists. It sounds like your code assumes that if the parameter exists, it also contains some useful value. That's not what isset() checks.

    In this case, the empty() function is probably what you're looking for. This will return true if a variable does not exist or if it contains a false-y value such as the empty string you're getting.

    Changing your if-statement to if (!empty($_GET['taxonomies'])) should do the trick.

    Keep in mind that the string "0" is also considered false-y and thus "empty", so if that's a valid value for one of your query parameters you'll need to add an explicit check for that.