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/
https://snipt.net/raw/424bc93629689d496b8b27c8c5748a06/
424bc93629689d496b8b27c8c5748a06
php
PHP
2
2016-09-29T23:38:04
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 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

PHP

Control Wordpress posts per page

1
2
3
4
5
6
    <?php
$page_num = $paged;
if ($pagenum='') $pagenum =1;
query_posts('showposts=20&paged='.$page_num); ?>
         
    
https://snipt.net/embed/d1230e43e996fccde11a5dfb423aed02/
https://snipt.net/raw/d1230e43e996fccde11a5dfb423aed02/
d1230e43e996fccde11a5dfb423aed02
php
PHP
6
2016-09-28T19:54:10
True
False
False
Aug 16, 2012 at 09:09 AM
/api/public/snipt/45775/
control-wordpress-posts-per-page
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="x"> </span><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">$page_num</span> <span class="o">=</span> <span class="nv">$paged</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a><span class="k">if</span> <span class="p">(</span><span class="nv">$pagenum</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="nv">$pagenum</span> <span class="o">=</span><span class="mi">1</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a><span class="nx">query_posts</span><span class="p">(</span><span class="s1">&#39;showposts=20&amp;paged=&#39;</span><span class="o">.</span><span class="nv">$page_num</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-5"><a name="L-5"></a><span class="x"> </span> </span><span id="L-6"><a name="L-6"></a><span class="x"> </span> </span></pre></div> </td></tr></table>
Wordpress, loop, php, posts

PHP

Display latest posts from a certain category

1
2
3
<?php $recent = new WP_Query("cat=3&showposts=3"); while($recent->have_posts()) : $recent->the_post();?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
https://snipt.net/embed/e62df76dabe04d6cc06ce4669da5132a/
https://snipt.net/raw/e62df76dabe04d6cc06ce4669da5132a/
e62df76dabe04d6cc06ce4669da5132a
php
PHP
3
2016-09-22T11:08:21
True
False
False
/api/public/snipt/39980/
display-latest-posts-from-a-certain-category
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="nv">$recent</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">WP_Query</span><span class="p">(</span><span class="s2">&quot;cat=3&amp;showposts=3&quot;</span><span class="p">);</span> <span class="k">while</span><span class="p">(</span><span class="nv">$recent</span><span class="o">-&gt;</span><span class="na">have_posts</span><span class="p">())</span> <span class="o">:</span> <span class="nv">$recent</span><span class="o">-&gt;</span><span class="na">the_post</span><span class="p">();</span><span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-2"><a name="L-2"></a><span class="x">&lt;li&gt;&lt;a href=&quot;</span><span class="cp">&lt;?php</span> <span class="nx">the_permalink</span><span class="p">();</span> <span class="cp">?&gt;</span><span class="x">&quot;&gt;</span><span class="cp">&lt;?php</span> <span class="nx">the_title</span><span class="p">();</span> <span class="cp">?&gt;</span><span class="x">&lt;/a&gt;&lt;/li&gt;</span> </span><span id="L-3"><a name="L-3"></a><span class="cp">&lt;?php</span> <span class="k">endwhile</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"category 1", php, posts, query
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.