2

Prepairing a function of similar posts faced the problem with wp_query. My args:

 $args = array(
  'post_type' => 'post',
  'post__not_in' => array(pll_get_post($post->ID,'ru'),pll_get_post($post->ID,'uk')),
  'posts_per_page' => $count,
  'caller_get_posts' => 1,

 );

 if (!empty($position_array) || !empty($cities_array) || !empty($ages_array)) {
  $args['tax_query'] = array(
   'relation' => 'OR',
   array(
    'taxonomy' => 'position',
    'field' => 'slug',
    'terms' => $position_array,
   ),
   array(
    'taxonomy' => 'city',
    'field' => 'slug',
    'terms' => $cities_array,
   ),
   array(
    'taxonomy' => 'age',
    'field' => 'slug',
    'terms' => $ages_array,
   ),
   array(
    'taxonomy' => 'post_tag',
    'field' => 'slug',
    'terms' => $tag_array
   )
  );
 };
 print_r($args);
 $my_query = new wp_query($args);

So it looks for posts similar by one of three custom taxonomies or tags, but must bypass current post in different languages (Polylang plugin).

If current language is default ('ru') function works good, but if i change language to 'uk', this function gives me similar posts and one of them is current post.

'post__not_in' => array(pll_get_post($post->ID,'ru'),pll_get_post($post->ID,'uk')),

- this returns proper ids (e.g. Array ( [0] => 331 [1] => 451 ) ). Why wp_query doesn't bypass current post in different languages?

  • What's the SQL that is being generated (see $my_query->request)? – janh Oct 07 '17 at 15:06
  • SELECT SQL_CALC_FOUND_ROWS twwxp_posts.ID FROM twwxp_posts LEFT JOIN twwxp_term_relationships ON (twwxp_posts.ID = twwxp_term_relationships.object_id) WHERE 1=1 AND twwxp_posts.ID NOT IN (331,331) AND ( twwxp_term_relationships.term_taxonomy_id IN (115) OR twwxp_term_relationships.term_taxonomy_id IN (116) OR twwxp_term_relationships.term_taxonomy_id IN (117) OR twwxp_term_relationships.term_taxonomy_id IN (225,239,241) ) AND twwxp_posts.post_type IN ('post', 'page') AND (twwxp_posts.post_status = 'publish') GROUP BY twwxp_posts.ID ORDER BY twwxp_posts.post_date DESC LIMIT 0, 4 – Stas Ponomaryov Oct 07 '17 at 17:19
  • Notice that it's transformed to `NOT IN (331,331)`, so the other one (451?) isn't excluded. This means you're not getting the right IDs with `pll_get_post($post->ID,'ru'),pll_get_post($post->ID,'uk')` – janh Oct 07 '17 at 17:24
  • but... Array ( [post_type] => post [post__not_in] => Array ( [0] => 331 [1] => 451 ) ... – Stas Ponomaryov Oct 07 '17 at 17:26
  • That's strange. what does var_dump($my_query) say? Maybe there's a filter somewhere that manipulates it in pre_get_posts or similar – janh Oct 07 '17 at 17:36
  • object(WP_Query)#4383 (49) { ["query"]=> array(5) { ["post_type"]=> string(4) "post" ["post__not_in"]=> array(1) { [0]=> array(2) { [0]=> int(331) [1]=> int(451) } } ... – Stas Ponomaryov Oct 07 '17 at 17:39
  • and I have 2 filters with pre_get_posts, but they manipulate in dashboard (filter author's media and posts) – Stas Ponomaryov Oct 07 '17 at 17:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156173/discussion-between-janh2-and-stas-ponomaryov). – janh Oct 07 '17 at 17:52

0 Answers0