WordPress.org

Forums

[resolved] tax_query and author_name won't play together in wp_query (6 posts)

  1. notkristina
    Member
    Posted 2 years ago #

    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?

  2. bcworkz
    Member
    Posted 2 years ago #

    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.

  3. notkristina
    Member
    Posted 2 years ago #

    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.

    <?php
    	$taxonomy_args = array('relation' => 'AND');
    	$meta_args = array('relation' => 'AND');
    	$location = array();
        $author = '';
    
    	if($status){
    		$status_args = array(
    					'taxonomy' => 'status',
    					'field' => 'slug',
    					'terms' => $status,
    					);
    		array_push($taxonomy_args, $status_args);
    	}
    	if($uses){
    		$uses_args = array(
    					'taxonomy' => 'uses',
    					'field' => 'slug',
    					'terms' => $uses,
    					);
    		array_push($taxonomy_args, $uses_args);
    	}
    	if($sqft){
    		$sqft_args = array(
    					'taxonomy' => 'sqft',
    					'field' => 'slug',
    					'terms' => $sqft,
    					);
    		array_push($taxonomy_args, $sqft_args);
    	}
    	foreach($location as $key => $value){
    		if($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',
    		'posts_per_page'=>-1,
    		'meta_query' => $meta_args,
    		'author_name' => $author,
    		'tax_query' => $taxonomy_args,
    	);
    	$nk_query = new WP_Query( $nk_query_args );
    ?>
  4. bcworkz
    Member
    Posted 2 years ago #

    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.

  5. notkristina
    Member
    Posted 2 years ago #

    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.

  6. notkristina
    Member
    Posted 2 years ago #

    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:
    http://vip.wordpress.com/documentation/list-posts-by-a-co-author/

Topic Closed

This topic has been closed to new replies.

About this Topic