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.