0

I have problem with post and taxonomy query. What i need is to get all post's from custom post type which starts with A and have defined term. Here is my sql:

SELECT * FROM $wpdb->posts, $wpdb->terms
    WHERE post_title LIKE 'А%'
    AND post_type = 'filmi-i-serialii'
    AND post_status = 'publish'
    AND slug = 'igralni';

But this returns me all posts in filmi-i-serialii post-type and not only those that are in the taxonomy igralni.

3
  • 1
    I've amended the question title to reflect what the question actually is, this may be more helpful for those looking in the listings for questions to answer
    – Tom J Nowell
    Commented May 23, 2013 at 13:46
  • when you look for tax igralni why do you ask your database for slug = 'igralni'; ?
    – hakre
    Commented May 23, 2013 at 13:53
  • I think you also might be interested ti learn about what a cross join means: en.wikipedia.org/wiki/Join_(SQL) - That is what you're doing here and most likely the cause of your problem.
    – hakre
    Commented May 23, 2013 at 13:58

2 Answers 2

0

Try this..

SELECT * 
FROM $wpdb->posts p 
INNER JOIN $wpdb->term_relationships tr 
  ON p.ID = tr.object_id
INNER JOIN $wpdb->term_taxonomy tt 
  ON tr.term_taxonomy_id=tt.term_taxonomy_id 
inner join  $wpdb->terms t 
  on t.term_id=tt.term_id and t.slug='igralni'
WHERE 
  p.post_title LIKE 'А%'        
  AND p.post_type = 'filmi-i-serialii' 
  AND p.post_status = 'publish'
4
  • Thank you for the fast answer but it's not working. It's returning 0.
    – Devoted
    Commented May 23, 2013 at 13:59
  • @Devoted do you have a post that starts with "A" and is of type "filmi-i-serialii" and is also in "igralni" category? check it out.. Commented May 23, 2013 at 14:01
  • Yes i have like 15. But this query returns 0
    – Devoted
    Commented May 23, 2013 at 14:03
  • @Devoted sorry my mistake, have edited answer, now try again it should work. Commented May 23, 2013 at 14:06
0

I would read this question as an attempt to restrict the query to posts that are in the taxonomy named igralni, as the question reads "only those that are in the taxonomy igralni", not "those that are tagged igralni". However, the accepted answer actually returns results from the post_tag taxonomy where the term is igralni.

Given that interpretation, this would be what I consider the right way to do it-- with a filter on posts_where. You may need more conditions but this is the basic idea.

function posts_by_letter_wpse_100462($where,$qry) {
  $ppl = $qry->get('posts_by_letter_where');
  if (!empty($ppl)) {
    $where .= " AND post_title LIKE '{$ppl}%' ";
  }
  return $where;
}
add_filter('posts_where','posts_by_letter_wpse_100462',1,2);

$args = array(
  'post_type' => 'filmi-i-serialii',
  'posts_per_page' => 10,
  'posts_by_letter_where' => 'm',
  'tax_query' => array(
    array(
      'taxonomy' => 'post_tag',
      'field' => 'slug',
      'terms' => 'igralni'
    )
  )
);
$wp_query = new WP_Query($args);

Notice that I added a parameter to WP_Query's arguments. Those are passed through unmodified but don't do anything unless you do something with them-- very convenient but undocumented and perhaps unintentional, so caution is advised.

If you really must do it with pure SQL, then run the query (with or without the filter), dump the SQL, steal it, and modify it.

var_dump($wp_query->request); die;
2
  • I'm doing it with ajax so the sql works perfect for that. :) But thank you i will save this code for latter use. Regards
    – Devoted
    Commented May 23, 2013 at 15:31
  • You should be using the AJAX API, in which case this would still be the correct answer.
    – s_ha_dum
    Commented May 23, 2013 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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