WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#26583 closed defect (bug) (duplicate)

urlencode issue with checkboxes and wp_query

Reported by: vk011 Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Query Keywords:
Focuses: Cc:

Description

Hello,

I am making a search that taps into wp_query and filters thru custom taxonomy terms. It works, but I get a urlencode warning when using checkboxes to filter thru taxonomy terms assigned to POSTS, but if for example I have the taxonomy terms assigned to CUSTOM POST TYPE, I DO NOT get the warning.

The following is my code, to replicate just copy/paste this into a file in the plugins folder and make custom taxonomies with some terms added to them (in this particular case they would be location , location2 and studio).

<?php

    /*
    Plugin Name: SS
    Plugin URI: http://wordpress.org/plugins/hello-dolly2/
    Description: Adds some search filtering
    Author: Dzimi
    Version: 1.0
    Author URI: http://ma.tt/
    */

    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', 'studio');

    foreach ($taxes as $taxonomy) {

        // echo gettype($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', 'studio');

            $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;

                    // echo '<pre>';
                    // print_r($tax_names_holder);
                    // echo '</pre>';
                }

            } // end foreach

            foreach ($tax_names_holder as $key => $value) {

                if (isset($_GET[$key])) {

                    $get_tax_term = $_GET[$key];

                    unset($tax_term);
                    foreach ($_GET[$key] as $value) {
                        $tax_term .= ','.$value;
                    }

                    $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');
    ?>

To get a detailed error report, I installed Development Debug Backtraces plugin, and what I get is:

#0  error_backtrace(2, urlencode() expects parameter 1 to be string, array given, C:\wamp\www\plugin_tester\wp-includes\formatting.php, 3645, Array ([path] => Array ([0] => bg),[suffix] => ))
#1  urlencode(Array ([0] => bg)) called at [C:\wamp\www\plugin_tester\wp-includes\formatting.php:3645]
#2  wp_basename(Array ([0] => bg)) called at [C:\wamp\www\plugin_tester\wp-includes\query.php:1749]
#3  WP_Query->parse_tax_query(Array ([s] => welcome,[location] => Array ([0] => bg),[error] => ,[m] => ,[p] => 0,[post_parent] => ,[subpost] => ,[subpost_id] => ,[attachment] => ,[attachment_id] => 0,[name] => ,[static] => ,[pagename] => ,[page_id] => 0,[second] => ,[minute] => ,[hour] => ,[day] => 0,[monthnum] => 0,[year] => 0,[w] => 0,[category_name] => ,[tag] => ,[cat] => ,[tag_id] => ,[author] => ,[author_name] => ,[feed] => ,[tb] => ,[paged] => 0,[comments_popup] => ,[meta_key] => ,[meta_value] => ,[preview] => ,[sentence] => ,[fields] => ,[menu_order] => ,[category__in] => Array (),[category__not_in] => Array (),[category__and] => Array (),[post__in] => Array (),[post__not_in] => Array (),[tag__in] => Array (),[tag__not_in] => Array (),[tag__and] => Array (),[tag_slug__in] => Array (),[tag_slug__and] => Array (),[post_parent__in] => Array (),[post_parent__not_in] => Array (),[author__in] => Array (),[author__not_in] => Array ())) called at [C:\wamp\www\plugin_tester\wp-includes\query.php:1577]
#4  WP_Query->parse_query() called at [C:\wamp\www\plugin_tester\wp-includes\query.php:2152]
#5  WP_Query->get_posts() called at [C:\wamp\www\plugin_tester\wp-includes\query.php:3243]
#6  WP_Query->query(Array ([s] => welcome,[location] => Array ([0] => bg))) called at [C:\wamp\www\plugin_tester\wp-includes\class-wp.php:541]
#7  WP->query_posts() called at [C:\wamp\www\plugin_tester\wp-includes\class-wp.php:605]
#8  WP->main() called at [C:\wamp\www\plugin_tester\wp-includes\functions.php:808]
#9  wp() called at [C:\wamp\www\plugin_tester\wp-blog-header.php:14]
#10 require(C:\wamp\www\plugin_tester\wp-blog-header.php) called at [C:\wamp\www\plugin_tester\index.php:17]


where 'bg' is a term inside location taxonomy.

I repeat, the warning DOES NOT occur with taxonomies attached to CUSTOM POST TYPES, just the regular POST, which is why I consider it a bug.

Please be so kind as to email me whether or not this is truly a bug and if it will be looked into or not and if you have additional questions to ask me feel free to email me or add me on skype: jultranet.

Thanks!


Change History (1)

#1 @SergeyBiryukov
8 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version changed from 3.8 to 3.1

Similar: #26308

The warning comes from wp_basename() in custom taxonomy handling block in WP_Query::parse_tax_query(): tags/3.8/src/wp-includes/query.php#L1748.

The code was introduced in [15732], modified in [15773], [15825], [15923], and [16155].

The patch on #17737 solves the issue by discarding a custom taxonomy query var if it's an array instead of a string.

Going to close as a duplicate of #17737 along with #26308 since it's a manifestation of the same issue.

Note: See TracTickets for help on using tickets.