Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm pretty sure I need to do some sort of custom query, but not sure about the best way to go with it.

I have an author page. It needs to display that author's posts. That's fine, I can do that. I need to amend the query so that it displays the author's posts as well as posts tagged to that author's name.

So I imagined the query something like this

query_posts("author=$author_name||tag=$author_name");

I figured that I could set up the query using WP_Query and feeding it the meta_query with the relation OR but that only returned posts by the author and ignored ones that were tagged:

 $args = array( 
   "showposts" => "$show_num_posts", 
   "meta_query" => array( 
   "relation" => "OR", 
        array(
          "author" => "$authID"),
        array(
          "tag" => "$nametag"
        )

   )
 );

 $the_query = new WP_Query($args);

I figure that it's ignoring it because it's found ones by the author and I've specified OR when what I really need is some sort of AND/OR. I've had a look at this example but can't seem to recreate that user's solution to fit my needs. I just keep breaking the whole thing.

global $wpdb;
$args = $wpdb->get_results($wpdb->prepare(
    "
    SELECT $wpdb->posts.*
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE  ($wpdb->posts.author = $authID)
    OR ($wpdb->postmeta.tag = $nametag)
    "
)
);
foreach ( $args as $post ) : setup_postdata($post);

Is there something out there you could point me towards? There has to be an easier way to do this. I'm probably over-complicating it and there's a really simple (probably in-built) way of doing it I've just missed. Searching the codex returned nothing useful though.

share|improve this question
    
Your meta_query is incomplete, which could be a problem, but I can't tell what you are trying to do. What does "tagged to that author's name" mean? How are the posts tagged? –  s_ha_dum May 21 '13 at 12:51
    
@s_ha_dum As in, a post by John or that's tagged 'John'. Does that make sense? –  nerdarama May 21 '13 at 13:13
    
No, still not clear. Are you talking about post tags? You've created individual post tags for your authors? –  s_ha_dum May 21 '13 at 13:53
    
Sorry, I mean that it could be a post written by John, or a post that's NOT written by John, but could be about him so is then tagged (using post tags) 'John'. –  nerdarama May 22 '13 at 9:28

1 Answer 1

up vote 2 down vote accepted

Your first query is very strange. You seem to be mixing meta_query parameters with other WP_Query arguments. You can't do that. author is not a valid component of a meta_query, neither is tag. The correct meta_query, if I am interpreting this right, wouldn't even be a meta query. You are dealing with a taxonomy, a tag, so you need a tax_query.

'tax_query' => array(
  array(
    'taxonomy' => 'post_tag',
    'field' => 'slug',
    'terms' => $nametag
  )
)

Unfortunately, if you build a complete query with an author, you get an AND between the tax query and the author, and you need an OR.

$query = new WP_Query( 
  array( 
    'author' => 1, 
    'tax_query' => array(
      array(
        'taxonomy' => 'post_tag',
        'field' => 'slug',
        'terms' => 'depo'
      )
    )
  )
);

That will give you the following query.

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
FROM wp_posts 
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
WHERE 1=1 
AND ( wp_term_relationships.term_taxonomy_id IN (57) ) 
AND (wp_posts.post_author = 1) 
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date DESC LIMIT 0, 5

That is not your data, but it is illustrative.

The AND right here-- AND (wp_posts.post_author = 1)-- is a problem. You need that to be an OR. In fact you need it in parenthesis too ...

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
FROM wp_posts 
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
WHERE 1=1 
AND ( 
  (wp_term_relationships.term_taxonomy_id IN (57)) 
  OR (wp_posts.post_author = 1) 
)
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date DESC LIMIT 0, 5

You would have to create a filter for posts_where to correct that, but since you are already writing your own SQL I will leave that alone. I think you can make things work using the query above as a reference.

share|improve this answer
    
This is great. Gives me a lot to build on. Thank you! –  nerdarama May 22 '13 at 9:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.