Hi All,
I hope someone can help!
I have 2 custom post types:
– Jobs
– Job Sectors
I have post object set with multiple values so you can add multiple sectors to a job
What I need to do and simply can’t fathom out is the ability to list all sectors and a count of how many jobs are in those sectors. For example:
– Charity (5)
– Retail (20)
– Marketing (1)
Any help is very much appreciated
Hi @jarvis
You can first get all the sectors by performing a get_posts
– http://codex.wordpress.org/Template_Tags/get_posts
Loop over these results, and for each sector display the title and link.
To find the count of jobs in this sector will require another get_posts
call for each sector.
The post_object (multi-select) saves it’s data in the same format of a relationship field (serialized array of IDs), so you can follow this tutorial to perform a backwards query: http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/
Thanks
E
Thanks @elliot for the reply.
I believe I’ve solved it. I’ve used the below to do what I need, not sure if there is a cleaner way though:
<ul>
<?php
$args = array(
'post_type' => 'job_sectors',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'name'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li><a href="<?php echo get_permalink($pageChild->ID);?>" title="<?php the_title(); ?>"><?php the_title(); ?><?php #the_ID(); ?></a>
<?php
global $post;
$args = array(
'post_type' => 'jobs',
'meta_query' => array(
array(
'key' => 'job_sector',
'value' => get_the_ID(),
'compare' => 'LIKE'
)
)
);
$myposts = get_posts( $args );
$total = 0;
foreach( $myposts as $post ) :
$total = $total + $post;
?>
<?php endforeach;
wp_reset_postdata();
echo "($total)";
?>
</li>
<?php endwhile; ?>
</ul>
Thanks