Support » Plugins and Hacks » Hacks » [Resolved] tax_query and author_name won't play together in wp_query

[Resolved] tax_query and author_name won't play together in wp_query

  • I’ve run up against a weird problem while writing a custom wp_query. I’m using author_name, tax_query and meta_query.

    Individually, my author_name parameter works.
    My tax_query parameter works.
    My meta_query parameter works.

    In combination, my tax_query and my meta_query parameters work when both specified;
    my meta_query and author_name parameters work when both specified.

    But when an author_name AND a tax_query are both specified, I get an empty set, even when there should be posts. Has anyone seen this before, and is there something I should look at to see what’s causing these two parameters to be incompatible?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Please post your query arguments code and any related variable assignments, someone may be able to spot something or at least explain what’s going on.

    Thanks bcworkz, good call.

    $status, $sqft, $uses and $location[] are brought in from a search form. They’re all optional, and fwiw, the query seems to have no problem being supplied with mostly-empty arrays when they aren’t provided.

    	$taxonomy_args = array('relation' => 'AND');
    	$meta_args = array('relation' => 'AND');
    	$location = array();
        $author = '';
    		$status_args = array(
    					'taxonomy' => 'status',
    					'field' => 'slug',
    					'terms' => $status,
    		array_push($taxonomy_args, $status_args);
    		$uses_args = array(
    					'taxonomy' => 'uses',
    					'field' => 'slug',
    					'terms' => $uses,
    		array_push($taxonomy_args, $uses_args);
    		$sqft_args = array(
    					'taxonomy' => 'sqft',
    					'field' => 'slug',
    					'terms' => $sqft,
    		array_push($taxonomy_args, $sqft_args);
    	foreach($location as $key => $value){
    			$field = '_listing_'.$key;
    			$locality_args = array(
    					'key' => $field,
    					'value' => $value,
    					'compare' => 'LIKE'
    			array_push($meta_args, $locality_args);
    	$nk_query_args = array(
    		'post_type' => 'listing',
    		'meta_query' => $meta_args,
    		'author_name' => $author,
    		'tax_query' => $taxonomy_args,
    	$nk_query = new WP_Query( $nk_query_args );

    In that code, $author is always an empty string from line 5, it is never assigned a valid value.

    Since the author_name query worked with meta_query alone, I imagine an important code segment got lost in the transition.

    My apologies for not mentioning—$author is also an optional string from the submission form. While it remains an empty string, wp_query seems perfectly happy to return posts by any author.

    In the end, I wasn’t taking into account the effects of the Co-Authors Plus plugin, and how I might need to refer to the post author differently. With the help of Daniel Bachhuber, the developer of that plugin, I was able to rewrite that portion of my wp_query argument correctly, and all’s now well.

    The solution:
    Enterprise WordPress Support, Training, Scaling and Hosting Services

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] tax_query and author_name won't play together in wp_query’ is closed to new replies.