snipt

Ctrl+h for KB shortcuts

PHP

Oredr posts by custom field

<?php query_posts($query_string. '&orderby=meta_value&meta_key=wpcf-datum-pocetka&order=ASC' ); ?>
https://snipt.net/embed/b077986fd382298797ee22885d4f2adf/
https://snipt.net/raw/b077986fd382298797ee22885d4f2adf/
b077986fd382298797ee22885d4f2adf
php
PHP
1
2016-09-24T00:10:45
True
False
False
/api/public/snipt/42829/
oredr-posts-by-custom-field
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</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="nx">query_posts</span><span class="p">(</span><span class="nv">$query_string</span><span class="o">.</span> <span class="s1">&#39;&amp;orderby=meta_value&amp;meta_key=wpcf-datum-pocetka&amp;order=ASC&#39;</span> <span class="p">);</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"custom fields", sort, wordpress

PHP

Display custom fields outside the loop

1
2
3
4
5
6
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();
?>
https://snipt.net/embed/36e90d0be3c05372538fe7262c6d251b/
https://snipt.net/raw/36e90d0be3c05372538fe7262c6d251b/
36e90d0be3c05372538fe7262c6d251b
php
PHP
6
2016-09-19T03:49:19
True
False
False
/api/public/snipt/26746/
display-custom-fields-outside-the-loop
<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="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="k">global</span> <span class="nv">$wp_query</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">$postid</span> <span class="o">=</span> <span class="nv">$wp_query</span><span class="o">-&gt;</span><span class="na">post</span><span class="o">-&gt;</span><span class="na">ID</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a><span class="k">echo</span> <span class="nx">get_post_meta</span><span class="p">(</span><span class="nv">$postid</span><span class="p">,</span> <span class="s1">&#39;Your-Custom-Field&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a><span class="nx">wp_reset_query</span><span class="p">();</span> </span><span id="L-6"><a name="L-6"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"custom fields", loop, wordpress

PHP

Show custom field only if populated

1
2
3
4
5
6
7
<?php $FIELDNAME = get_post_meta($post->ID, 'FIELDNAME', true);
	
	if($FIELDNAME) : ?>

	<a title="Click Here To Purchase Tickets" href="<?php get_custom_field_value('FIELDNAME', true) ?>" rel="bookmark">PURCHASE TIX</a>

	<?php endif; ?>
https://snipt.net/embed/4d0b4fe0c7027b3bfa6e28fddf4509a7/
https://snipt.net/raw/4d0b4fe0c7027b3bfa6e28fddf4509a7/
4d0b4fe0c7027b3bfa6e28fddf4509a7
php
PHP
7
2016-09-23T19:37:27
True
False
False
/api/public/snipt/12206/
show-custom-field-only-if-populated
<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> <a href="#L-7">7</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">$FIELDNAME</span> <span class="o">=</span> <span class="nx">get_post_meta</span><span class="p">(</span><span class="nv">$post</span><span class="o">-&gt;</span><span class="na">ID</span><span class="p">,</span> <span class="s1">&#39;FIELDNAME&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a> <span class="k">if</span><span class="p">(</span><span class="nv">$FIELDNAME</span><span class="p">)</span> <span class="o">:</span> <span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="x"> &lt;a title=&quot;Click Here To Purchase Tickets&quot; href=&quot;</span><span class="cp">&lt;?php</span> <span class="nx">get_custom_field_value</span><span class="p">(</span><span class="s1">&#39;FIELDNAME&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">)</span> <span class="cp">?&gt;</span><span class="x">&quot; rel=&quot;bookmark&quot;&gt;PURCHASE TIX&lt;/a&gt;</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a><span class="x"> </span><span class="cp">&lt;?php</span> <span class="k">endif</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"custom fields", php, wordpress
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.