Home › Forums › General Issues › Sorting by Custom Fields, with Priority
I’m working on an employee directory for a site that is running ACF 3.5.8.2 (kind of stuck with this version at the moment).
What I’m trying to do is sort this directory in two ways. I have custom fields called ‘dir-last-name’ (text) and ‘dir-department-head’ (checkbox).
If someone is listed as a Department Head, I want to list them first in their respective department.
Then, for everyone else in that department, I want to sort them alphabetically by their Last Name.
I read through this post, which was helpful:
http://www.advancedcustomfields.com/resources/how-to-query-posts-filtered-by-custom-field-values/
However, what I’m trying to figure out is how to ensure that the Department Head will get listed first. Any suggestions?
One possible route is to query for the Department Head first, and then do a second query to get the remaining posts excluding the Department Head post id. It would be using the exclude
parameter from get_posts
– http://codex.wordpress.org/Template_Tags/get_posts
Thanks! I have thought about doing 2 queries but I’m trying to salvage the setup of the their template if possible.
Right now I’ve got a query like this:
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => -1,
'meta_key' => 'dir-last-name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
query_posts($args);
?>
So that is grabbing everyone’s last name and sorting it. The template also has this bit of code, which I’m hoping to modify:
<?php if (in_array("Administration", (array) get_field('dir-department'))): ?>
In this bit, it’s looking for entries in the Administration department. Code below that (not included here) formats the entry nicely.
What I would love to do is modify this to also look for a value of “yes” in the ‘dir-department-head’ field. So I want to look for someone in the Administration department (like the code above) and then also check to see if they are a Department Head. Basically an if/else statement that checks for a Department Head, then moves on to the rest of the list.
I know only enough PHP to be dangerous so everything I’ve tried to do doesn’t seem to work 😉
Ah, maybe like:
<?php if (in_array("Administration", (array) get_field('dir-department')) || ('yes' == get_field('dir-department-head'))): ?>
ractoon, thanks for your help! That code led me in the right direction. I did end up having to use 2 queries.
First I checked for department heads:
<?php //Get the Department Head First ?>
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'staff',
'meta_key' => 'dir-department-head',
'meta_value' => 'yes'
);
query_posts($args);
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (in_array("Administration", (array) get_field('dir-department')) AND ('yes' == get_field('dir-department-head'))): ?>
Formatting fields, etc.
<?php endif; ?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
Then, a separate query for the rest of the employees, excluding the “yes” answers in the department head field by pulling only those entries who left the field blank:
<?php // Get Other Deparment Employees ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => -1,
'meta_key' => 'dir-last-name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
query_posts($args);
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (in_array("Administration", (array) get_field('dir-department')) AND ('' == get_field('dir-department-head'))): ?>
Formatting fields, etc.
<?php endif; ?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
Thanks for leading me in the right direction!
The topic ‘Sorting by Custom Fields, with Priority’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.