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 (http://newsite.minationalguard.com/vacancies/) 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:

    http://www.advancedcustomfields.com/resources/query-posts-custom-fields/

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

    https://wordpress.org/plugins/wp-custom-fields-search/

    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!

    Cheers,
    Adam

  • 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.

    <?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">
    					<tbody>
    						<tr>
    							<td>Select Category</td>
    							<td colspan='3'>
    
    								<?php
    								//dropdown category select
    								wp_dropdown_categories(); ?></td>
    						</tr>
    						<tr>
    							<th>Title</th>
    							<th>Category</th>
    							<th>Information</th>
    							<th>Expires</th>
    						</tr>
    
    				<?php while( $the_query->have_posts() ) : $the_query->the_post(); 
    
    								$class = get_field('closing_date'); ?>
    
    						<tr>
    							<td><strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></strong></td>
    							<td><?php $category = get_the_category(); echo $category[0]->cat_name; ?></td>
    							<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>
    							<td><?php the_field('closing_date'); ?></td>
    						</tr>
    
    				<?php endwhile; ?>
    
    					</tbody>
    
    				</table>
    
    		<?php endif;
    
    		wp_reset_query();
    }
    
    /** Remove Post Info */
    remove_action('genesis_before_post_content','genesis_post_info');
    remove_action('genesis_after_post_content','genesis_post_meta');
    
    genesis();
  • 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;
    		-->
    	</script>

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

    if(isset($_GET['cat'])&&$_GET['cat']!=""){
    	$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'
    		));
    }else{
    $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.