-1

Can someone please show me how I can achieve the same result with this SQL:

SELECT *
FROM wp_2_posts
INNER JOIN wp_2_icl_translations
ON wp_2_icl_translations.element_id = wp_2_posts.id
AND wp_2_icl_translations.language_code = 'en'
WHERE wp_2_posts.post_type = 'properties';

Using posts_clauses ? In other words I'd like a posts_clauses filter to perform the same query as the one shown above in SQL.

I am trying to query posts that are only in English.

1 Answer 1

-1

First, his filter is explained in the manual and is found in the code. If you have questions about hooks and functions, the manual and code is a good place to start looking for answers. The post_clauses hook is passed the associative array to filter, so just manipulate the where and join indices as per your needs. (As per the manual and code, this filter cannot modify table name.)

Also, this kind of question has already been answered, and I'd like to direct your attention to that discussion for coding examples.

UPDATE: OK, @robskrob, here you go! Again, this will not initialize the query object to be blank nor will it filter the table name.

add_filter('post_clauses', function($clauses){
    $clauses['join'] .= " INNER JOIN wp_2_icl_translations ON wp_2_icl_translations.element_id = wp_2_posts.id AND wp_2_icl_translations.language_code = 'en'";
    $clauses['where'] .= " AND wp_2_posts.post_type = 'properties'";
    return $clauses;
});
5
  • 1
    This sentence to me sounds like it is designed to shame more than it is to inform or help: "A guy who is a beginner can find many answers in these two sources." Also I think you should exercise more gender neutral language to be more inclusive. So maybe just strike out the sentence entirely seeing that on the whole it's inappropriate?
    – robskrob
    Commented May 29, 2020 at 17:18
  • Thanks, @robskrob, I didn't recognize the tone was shaming, and didn't mean it to be. I've changed it up and hopefully its better.
    – Mort 1305
    Commented May 29, 2020 at 17:46
  • yes your answer is much more appropriate now, in my opinion. Thank you for cleaning it up. I still think the preferred answer is in fact an actual answer to my question which provides a PHP example using my SQL with the posts_clause. I have the SQL. Someone with Wordpress experience and familiarity with that filter could probably just apply the filter's API with my SQL. I'm all for reading the docs and reading source. However, I have a day job, and sometimes I'm under the gun so time is lacking. I think an actual answer to my question would be useful to the community as well
    – robskrob
    Commented May 29, 2020 at 21:30
  • Thanks for the heads up on the wording; if you took it the wrong say, someone else probably did too. So, you want the actual code to just copy and paste, eh? OK, let me update my answer for you: should take a minute or two to copy what you have and paste it in place. (All done. I've got to get back to work now.)
    – Mort 1305
    Commented May 29, 2020 at 23:19
  • Thank you Mort! As you can imagine, instead of waiting around for an answer for 2 days, I found a different solution that works for me. Here's the link: wordpress.stackexchange.com/a/367813/135177 -- I think the answer you provided above will be useful for someone with a similar problem. Also, who knows? I may even reference it later down the line.
    – robskrob
    Commented May 30, 2020 at 20:09

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.