Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I filter a custom posts by a field in wordpress?
    primarykey
    data
    text
    <p>OK, very simple task, I'm just not good at PHP.</p> <p>I have a page where I want to list some staff using a styled list. Here is the page - <a href="http://www.themontessoripeople.co.uk/montesori/?post_type=people" rel="nofollow">http://www.themontessoripeople.co.uk/montesori/?post_type=people</a> </p> <p>I downloaded a "custom content type" plugin and added the content type of "people" and added the appropriate fields. Now I want to filter the posts I have added by the custom field called "hierarchy".</p> <p>Here is how I want the page to display - <a href="http://i47.tinypic.com/oqymwh.jpg" rel="nofollow">http://i47.tinypic.com/oqymwh.jpg</a></p> <p>The custom field "hierarchy" contains the room variables of either "management", "babies_room" and "toddlers_room".</p> <p>How do I amend the code below to filter the posts by the value held within <code>&lt;?php print_custom_field('hierarchy'); ?&gt;</code> ?</p> <pre><code>&lt;?php $col = 1; ?&gt; &lt;?php if (have_posts()) : ?&gt; &lt;?php while (have_posts()) : the_post(); ?&gt; &lt;?php if ($col == 1) echo "&lt;div class=\"row\"&gt;"; ?&gt; &lt;div class="post col&lt;?php echo $col;?&gt;" id="post-&lt;?php the_ID(); ?&gt;"&gt; &lt;div class="people-spacer"&gt; &lt;div class="people"&gt;&lt;a class="animate" &gt; &lt;div class="bio"&gt; &lt;p class="titles"&gt;&lt;?php the_title(); ?&gt;&lt;br/&gt; &lt;span class="job"&gt; &lt;?php print_custom_field('job'); ?&gt;&lt;/span&gt; &lt;/p&gt;&lt;br /&gt; &lt;/div&gt; &lt;img src="&lt;?php print_custom_field('staff_image:to_image_src'); ?&gt;" width="160" height="160" alt="&lt;?php the_title(); ?&gt;-image" /&gt; &lt;/div&gt; &lt;div class="people-link-edit"&gt;&lt;?php edit_post_link('Edit Post', ''); ?&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;?php if ($col == 1) echo "&lt;/div&gt;"; (($col==1) ? $col=2 : $col=2); ?&gt; &lt;?php endwhile; ?&gt; </code></pre> <p>Thanks, Ben.</p> <p>Here is the working code showing two sets of filtered results for reference - </p> <pre><code>&lt;?php $col = 1; ?&gt; &lt;?php if (have_posts()) : ?&gt; &lt;div class="text-box"&gt; &lt;h2&gt;Management&lt;/h2&gt; &lt;?php while (have_posts()) : the_post(); ?&gt; &lt;?php if (get_custom_field('hierarchy') != "management") continue; ?&gt; &lt;?php if ($col == 1) echo "&lt;div class=\"row\"&gt;"; ?&gt; &lt;div class="post col&lt;?php echo $col;?&gt;" id="post-&lt;?php the_ID(); ?&gt;"&gt; &lt;div class="people-spacer"&gt; &lt;div class="people"&gt;&lt;a class="animate" &gt; &lt;div class="bio"&gt; &lt;p class="titles"&gt; &lt;?php the_title(); ?&gt; &lt;br/&gt; &lt;span class="job"&gt; &lt;?php print_custom_field('job'); ?&gt;&lt;/span&gt; &lt;/p&gt; &lt;br /&gt; &lt;/div&gt; &lt;img src="&lt;?php print_custom_field('staff_image:to_image_src'); ?&gt;" width="160" height="160" alt="&lt;?php the_title(); ?&gt;-image" /&gt; &lt;/div&gt; &lt;div class="people-link-edit"&gt; &lt;?php edit_post_link('Edit Post', ''); ?&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;?php if ($col == 1) echo "&lt;/div&gt;"; (($col==1) ? $col=2 : $col=2); ?&gt; &lt;?php endwhile; ?&gt; &lt;/div&gt;&lt;!-- close text box --&gt; &lt;div class="text-box"&gt; &lt;h2&gt;Babies Room&lt;/h2&gt; &lt;?php while (have_posts()) : the_post(); ?&gt; &lt;?php if (get_custom_field('hierarchy') != "babies_room") continue; ?&gt; &lt;?php if ($col == 1) echo "&lt;div class=\"row\"&gt;"; ?&gt; &lt;div class="post col&lt;?php echo $col;?&gt;" id="post-&lt;?php the_ID(); ?&gt;"&gt; &lt;div class="people-spacer"&gt; &lt;div class="people"&gt;&lt;a class="animate" &gt; &lt;div class="bio"&gt; &lt;p class="titles"&gt; &lt;?php the_title(); ?&gt; &lt;br/&gt; &lt;span class="job"&gt; &lt;?php print_custom_field('job'); ?&gt;&lt;/span&gt; &lt;/p&gt; &lt;br /&gt; &lt;/div&gt; &lt;img src="&lt;?php print_custom_field('staff_image:to_image_src'); ?&gt;" width="160" height="160" alt="&lt;?php the_title(); ?&gt;-image" /&gt; &lt;/div&gt; &lt;div class="people-link-edit"&gt; &lt;?php edit_post_link('Edit Post', ''); ?&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;?php if ($col == 1) echo "&lt;/div&gt;"; (($col==1) ? $col=2 : $col=2); ?&gt; &lt;?php endwhile; ?&gt; &lt;/div&gt;&lt;!-- close text box --&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload