• [Moderator note: moved to How-To and Troubleshooting]

    $line[0]="Santa's Wild Ride";
    $query = new WP_Query(array('post_type'=>'it_games',
    				'tax_query' => array(
    					array(
    						'taxonomy' => 'game_name',
    						'field'    => 'name',
    						'terms'    => stripslashes_deep($line[0])
    					)
    				)
    	));
    $id=0;
    if($query->have_posts()) $id=$query->post->ID;
    echo $id;

    That code does not find posts. But I checked in admin panel there’s post with this term in this taxonomy.

    I use exactly same code to find games which names don’t contain apostrophe and it works!
    But it also does not work for terms with symbols “(“, “)”.
    What am I doing wrong?

    • This topic was modified 8 years, 2 months ago by Expert1.
    • This topic was modified 8 years, 2 months ago by bdbrown.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You’ve done nothing wrong. It looks like you’ve stumbled upon a bug in WordPress core! The tax_query parser ends up double escaping name term arguments, so your term ends up being “Santa\\\’s Wild Ride” instead of the proper “Santa\’s Wild Ride”.

    A workaround until this is fixed in core would be to add a filter to strip one set of slashes:

    // fix tax_query bug that double escapes name terms
    add_filter('pre_term_name', function( $term ) { return stripslashes( $term ); }, 40 );

    You had the right idea to strip slashes, but doing it in the tax_query argument is too early, no slashes have been added at this point. (Unless your script potentially could be passing already escaped terms) I’m going to go ahead and file a Trac ticket since I have all the pertinent details, but I’ll certainly give you credit for finding the bug.

    EDIT: The Trac ticket is #39315. You can click Watch this Ticket button to get email notifications when anyone posts a comment.

    • This reply was modified 8 years, 2 months ago by bcworkz. Reason: added Trac link
    Thread Starter Expert1

    (@expert1)

    Thank you very much, it helped!
    But still does not work with symbols: (, ), [, ]
    I tried addslashes($line[0]) in wp_query args, but still no luck.

    Moderator bcworkz

    (@bcworkz)

    Hmmm, something else is going on then. Queries with ()[] work fine on my installation. They are unaffected by esc_sql(), addslashes(), etc., so the bug and my workaround would have no effect one way or another.

    There must be some other code influencing your queries. You can try narrowing down the source by switching to a twenty* theme and deactivating all plugins, (the query should work now) then restoring each one by one until the query fails again.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_Query does not fetch posts’ is closed to new replies.