WordPress.org

Ready to get started?Download WordPress

Forums

urlencode problem with tax_query and checkboxes (1 post)

  1. vk011
    Member
    Posted 4 months ago #

    I am tapping into regular wp search by adding filtering thru custom taxonomies via pre_get_posts action.

    The problem is that if I have checkboxes, I get the following error (even though the filter itself works) - Warning: urlencode() expects parameter 1 to be string, array given in C:\wamp\www\plugin_tester\wp-includes\formatting.php on line 3318

    Also, this only happens if the taxonomy is assigned to POST post type. If I assign a taxonomy to a custom post type, I don't get the warning.

    function vss_form() {
    $blog_url = get_bloginfo('url');
        ?>
    <form method="get" action="<?php echo $blog_url; ?>" id="ss-search" name="vkss-config">
    <input type="text" value="<?php echo 'welcome'; ?>" name="s" />
    <?php
    
    $taxes = array('location', 'location2');
    
    foreach ($taxes as $key => $taxonomy) {
    
        $terms = get_terms($taxonomy, array('orderby' => 'id', 'order' => 'ASC'));
            foreach ($terms as $term) {
                $term_name = $term->name;
                echo "<p><input type=\"checkbox\" name=\"{$taxonomy}[]\" value=\"$term_name\" id=\"$term_name\" /><label for=\"$term_name\"> $term_name</label></p>";
            }
    }
    
    ?>
    <button>SS Submit</button>
    </form>
    <?php
    }
    function vss_search($query) {
    
        // has the form been init?
        if (isset($_GET['s'])) {
    
            //make the query
            $s = filter_input(INPUT_GET, 's', FILTER_SANITIZE_STRING);
            // if ( empty( $s ) ) return;
            $query->is_search = true;
            // $query->set( 's', $s );     
    
            $tax_holder = array();
    
            $all_taxes = array('location', 'location2');
    
            $tax_names_holder = array();
    
            foreach ($all_taxes as $ind_tax_name) {
    
                if (isset($_GET[$ind_tax_name])) {
    
                    $cb_tax_name = $_GET[$ind_tax_name];
    
                    $tax_names_holder[$ind_tax_name] = $cb_tax_name;
                }
    
            } 
    
            foreach ($tax_names_holder as $key => $value) {
    
                if (isset($_GET[$key])) {
    
                    $get_tax_term = $_GET[$key];
    
                    unset($tax_term);
                    foreach ($get_tax_term as $term) {
                        $tax_term .= ','.$term;
                    }
    
                    $tax_term = ltrim($tax_term, ',');
    
                    $new_tax_term = explode(',', $tax_term);
    
                    $tax_holder[] = array(
                    'taxonomy' => $key,
                    'field' => 'slug',
                    'operator' => 'AND',
                    'terms' => $new_tax_term
                    );
    
                } // end if (isset($_GET[$key])) {
    
            } // end foreach
            // end get tax checkbox results
    
            $query->set( 'tax_query', $tax_holder);
            // echo '<pre>';
            // print_r($tax_holder);
            // echo '</pre>';
    
        } // end has the form been init?
    
    } // end vss_search()
    add_action('pre_get_posts', 'vss_search');
    
    add_shortcode('vss', 'vss_form')

Reply

You must log in to post.

About this Topic