WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#27810 closed defect (bug) (fixed)

wp_query taxonomy query by name doesn't seem to work if there's a space in the name

Reported by: neonWired Owned by: boonebgorges
Milestone: 4.2 Priority: normal
Severity: normal Version: 3.9
Component: Taxonomy Keywords:
Focuses: Cc:

Description

if you add a taxonomy query to wp_query, for example:

$args['tax_query'] = array(
			array(
				'taxonomy' => 'careercategory',
				'field' => 'name',
				'terms' => array('Business Manager')
			)
		);

It doesn't work if there's a space in the term. e.g. 'Business Manager' in this example.

The expected output is all posts with the term 'Business Manager', the actual output is empty.

Change History (14)

#1 @neonWired
5 years ago

  • Resolution set to invalid
  • Status changed from new to closed

#2 @neonWired
5 years ago

Sorry, not a bug. Resolved.

#3 @DrewAPicture
5 years ago

  • Milestone Awaiting Review deleted

#4 follow-up: @jeffmcmahan
4 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Could someone explain how this is an invalid bug? That the observed behavior could be considered acceptable strikes me as hard to believe.

It's an easy fix in core:

If query is by name and the string is not numeric, pass it to sanitize_title() and then run the query by slug. That'll work in every case except where there are duplicate names (and in that case, one cannot complain).

Last edited 4 years ago by jeffmcmahan (previous) (diff)

#5 follow-up: @boonebgorges
4 years ago

  • Component changed from Query to Taxonomy
  • Milestone set to 4.2

It's not invalid. I've confirmed the bug. The issue is that the type of sanitization that's taking place in WP_Tax_Query does not match the type of sanitization that happens when terms are inserted into the database. In this particular case, that means that Foo Bar Baz is being turned into foo-bar-baz for the WHERE clause.

If query is by name and the string is not numeric, pass it to sanitize_title() and then run the query by slug. That'll work in every case except where there are duplicate names (and in that case, one cannot complain).

No, because it's possible to create a term with a name that is totally unrelated to the slug. The correct fix is to use sanitize_term_field() instead of sanitize_title_for_query() inside of WP_Tax_Query::transform_query().

#6 in reply to: ↑ 4 @helen
4 years ago

Replying to jeffmcmahan:

Could someone explain how this is an invalid bug? That the observed behavior could be considered acceptable strikes me as hard to believe.

Just to note that I don't think anybody investigated this originally - the reporter is the one who closed it.

#7 @boonebgorges
4 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from reopened to closed

In 31346:

Use field-specific sanitization in WP_Tax_Query::transform_query().

When terms are entered into the database, term fields are sanitized with
sanitize_term_field(). To ensure that the SELECT ... WHERE queries in
WP_Tax_Query::transform_query() are not broken by overzealous sanitization,
sanitize_term_field() should be used in that case as well. This fixes a bug
where a tax_query using 'field=name' would fail if the 'terms' parameter
contained characters (like spaces) that were improperly removed by
sanitize_title_for_query().

Fixes #27810.

#8 in reply to: ↑ 5 @jeffmcmahan
4 years ago

Replying to boonebgorges:

If query is by name and the string is not numeric, pass it to sanitize_title() and then run the query by slug. That'll work in every case except where there are duplicate names (and in that case, one cannot complain).

No, because it's possible to create a term with a name that is totally unrelated to the slug. The correct fix is to use sanitize_term_field() instead of sanitize_title_for_query() inside of WP_Tax_Query::transform_query().

Oh that's right - I forgot about the direct editing of slugs.

#9 @manifestcreative
4 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

#10 @boonebgorges
4 years ago

@manifestcreative - Could you please leave a comment to explain why you reopened this ticket? Any information you can provide would be useful.

#11 @DrewAPicture
4 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

No reason was provided for why this was reopened three weeks ago. Re-closing as fixed.

#12 @tjtate
4 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

This is my first time on here so forgive me but this looks to still be an issue.

Code highlighting:

                $args = array(
                        'post_type' => 'products',
                        'tax_query' => array(
                                array(
                                        'taxonomy' => 'product_categories',
                                        'field'    => 'name',
                                        'terms'    =>  array('test this'),
                                ),
                        ),
                );
                $queryz = new WP_Query( $args );

produces:

Code highlighting:

WP_Query Object
(
    [query] => Array
        (
            [post_type] => products
            [tax_query] => Array
                (
                    [0] => Array
                        (
                            [taxonomy] => product_categories
                            [field] => name
                            [terms] => Array
                                (
                                    [0] => test this
                                )

                        )

                )

        )

    [query_vars] => Array
        (
            [post_type] => products
            [tax_query] => Array
                (
                    [0] => Array
                        (
                            [taxonomy] => product_categories
                            [field] => name
                            [terms] => Array
                                (
                                    [0] => test this
                                )

                        )

                )

            [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] => 
            [s] => 
            [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
                (
                )

            [ignore_sticky_posts] => 
            [suppress_filters] => 
            [cache_results] => 1
            [update_post_term_cache] => 1
            [update_post_meta_cache] => 1
            [posts_per_page] => 10
            [nopaging] => 
            [comments_per_page] => 50
            [no_found_rows] => 
            [taxonomy] => product_categories
            [term_id] => test this
            [order] => DESC
        )

    [tax_query] => WP_Tax_Query Object
        (
            [queries] => Array
                (
                    [0] => Array
                        (
                            [taxonomy] => product_categories
                            [terms] => Array
                                (
                                    [0] => test this
                                )

                            [field] => name
                            [operator] => IN
                            [include_children] => 1
                        )

                )

            [relation] => AND
            [table_aliases:protected] => Array
                (
                )

            [queried_terms] => Array
                (
                    [product_categories] => Array
                        (
                            [terms] => Array
                                (
                                    [0] => test this
                                )

                            [field] => name
                        )

                )

            [primary_table] => wp_posts
            [primary_id_column] => ID
        )

    [meta_query] => WP_Meta_Query Object
        (
            [queries] => Array
                (
                )

            [relation] => 
            [meta_table] => 
            [meta_id_column] => 
            [primary_table] => 
            [primary_id_column] => 
            [table_aliases:protected] => Array
                (
                )

        )

    [date_query] => 
    [request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND ( 
  0 = 1
) AND wp_posts.post_type = 'products' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
    [posts] => Array
        (
        )

    [post_count] => 0
    [current_post] => -1
    [in_the_loop] => 
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 1
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 
    [is_tag] => 
    [is_tax] => 1
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 
    [is_404] => 
    [is_comments_popup] => 
    [is_paged] => 
    [is_admin] => 
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 
    [is_post_type_archive] => 1
    [query_vars_hash:WP_Query:private] => 886139e04595cf967401d61fae194ccc
    [query_vars_changed:WP_Query:private] => 
    [thumbnails_cached] => 
    [stopwords:WP_Query:private] => 
)

Notice the request isnt looking up the taxonomy at all

#13 @tjtate
4 years ago

  • Resolution set to worksforme
  • Status changed from reopened to closed

nvrmnd, applied the fix in 31346 and it worked ... ... .

#14 @dd32
4 years ago

  • Resolution changed from worksforme to fixed
Note: See TracTickets for help on using tickets.