Sort posts by category in table

I am not looking for a plugin recommendation so much as a code snippet.

I created a job board ( with Advanced Custom Fields. I put the posts in a table, organized by an expiration date ACF field. Works fine to show all categories, but need to be able to filter by category. The dropdown category selector is just echoes right now; there is not code beyond just the display.

  • Adam Czajczyk

    Hey Josh,

    I hope you're having a great day! You've got a great site, it's really clear and easy to read!

    The ACF plugin it's just an advanced interface that makes it easy to create and manage native WordPress custom fields. That means that those fields should be easily searchable with the standard WP methods. However, the way ACF handles custom fields might be a bit confusing sometimes. Luckily, there's a good instruction on searching ACF-created custom fields here:

    I think you might want to take a look at this plugin here:

    It allows you to build a custom search form based on a custom fields, so it should be of help here or at least give you some ideas.

    I hope that helps and if you have any further questions on this, please ask. I'll be glad to assist!


  • Josh

    Thanks Adam. The ACF link cleared some things up, but I'm still missing something.

    What I think...The ACF side is already working for me. The custom fields are displaying where I want them to in my custom loop, and are being ordered by expiration date. The only thing I need to do is filter my table to display specific category posts. No matter what I try, I always get a full listing of all posts from all categories. (The wp_dropdown_categories(); is obviously incomplete. I just don't know what to put in there.)

    I could be off on my understanding, I just don't know where. Here is my archive-job.php.

     * Template Name: Job Archives
     * Description: Used as a page template to show page contents, followed by a loop through a CPT archive
    //* Reposition the breadcrumbs
    remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
    remove_action ('genesis_loop', 'genesis_do_loop'); // Remove the standard loop
    add_action( 'genesis_loop', 'custom_do_grid_loop' ); // Add custom loop
    function custom_do_grid_loop() {  
    			// query
    		$the_query = new WP_Query(array(
    			'post_type'	  		=> 'job',
    			'posts_per_page'	=> 200,
    			'meta_key'			         => 'closing_date',
    			'orderby'			                 => 'meta_value_num',
    			'order'	          			=> 'ASC'
    		if( $the_query->have_posts() ): ?>
    			<table class="employment">
    							<td>Select Category</td>
    							<td colspan='3'>
    								//dropdown category select
    								wp_dropdown_categories(); ?></td>
    				<?php while( $the_query->have_posts() ) : $the_query->the_post(); 
    								$class = get_field('closing_date'); ?>
    							<td><strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></strong></td>
    							<td><?php $category = get_the_category(); echo $category[0]->cat_name; ?></td>
    								<strong>Number:</strong> <?php the_field('job_number'); ?><br>
    								<strong>Grade/Rank:</strong> <?php the_field('wage'); ?><br>
    								<strong>Location:</strong> <?php the_field('job_location');?>, Michigan
    							<td><?php the_field('closing_date'); ?></td>
    				<?php endwhile; ?>
    		<?php endif;
    /** Remove Post Info */
  • Anang

    Hi There,

    Trying to help here :slight_smile: Could you put this code below wp_dropdown_categories() put it outside php tag since it's javascript tags

    <script type="text/javascript">
    		var dropdown = document.getElementById("cat");
    		function onCatChange() {
    			if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
    				location.href = "<?php echo esc_url( home_url( '/vacancies' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
    		dropdown.onchange = onCatChange;

    And then put this code to made filter by category if ?cat parameter is exist in URL (replace $the_query line ) :

    	$the_query = new WP_Query(array(
    			'post_type'	  		=> 'job',
            'cat' =>$_GET['cat'],
    			'posts_per_page'	=> 200,
    			'meta_key'			         => 'closing_date',
    			'orderby'			                 => 'meta_value_num',
    			'order'	          			=> 'ASC'
    $the_query = new WP_Query(array(
    			'post_type'	  		=> 'job',
    			'posts_per_page'	=> 200,
    			'meta_key'			         => 'closing_date',
    			'orderby'			                 => 'meta_value_num',
    			'order'	          			=> 'ASC'

    Let me know the result :slight_smile:
    Best Regards

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.