snipt

Ctrl+h for KB shortcuts

PHP

Wordpress query Join for post - taxonomy - term

$sql="SELECT DISTINCT * FROM wp_posts INNER JOIN wp_term_relationships ON(wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_taxonomy ON(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) INNER JOIN wp_terms ON(wp_term_taxonomy.term_id = wp_terms.term_id) WHERE (wp_terms.name LIKE '% {$search_keyword}%' OR wp_terms.name LIKE '{$search_keyword}%' OR wp_posts.post_title LIKE '% {$search_keyword}%' OR wp_posts.post_title LIKE '{$search_keyword}%') AND wp_posts.post_status='publish'";
$search_products = $wpdb->get_results($sql, OBJECT);
https://snipt.net/embed/424bc93629689d496b8b27c8c5748a06/
/raw/424bc93629689d496b8b27c8c5748a06/
424bc93629689d496b8b27c8c5748a06
php
PHP
2
2019-07-23T11:18:22
True
False
False
Jan 27, 2013 at 08:04 AM
/api/public/snipt/54584/
wordpress-query-join-for-post-taxonomy-term
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="x">$sql=&quot;SELECT DISTINCT * FROM wp_posts INNER JOIN wp_term_relationships ON(wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_taxonomy ON(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) INNER JOIN wp_terms ON(wp_term_taxonomy.term_id = wp_terms.term_id) WHERE (wp_terms.name LIKE &#39;% {$search_keyword}%&#39; OR wp_terms.name LIKE &#39;{$search_keyword}%&#39; OR wp_posts.post_title LIKE &#39;% {$search_keyword}%&#39; OR wp_posts.post_title LIKE &#39;{$search_keyword}%&#39;) AND wp_posts.post_status=&#39;publish&#39;&quot;;</span> </span><span id="L-2"><a name="L-2"></a><span class="x">$search_products = $wpdb-&gt;get_results($sql, OBJECT);</span> </span></pre></div> </td></tr></table>
join, mysql, posts, query, taxonomy, wordpress