Forums Forums Search & Filter Pro Custom Results Page – Order results by ACF value

Viewing 5 posts - 1 through 5 (of 5 total)
  • Anonymous
    #228401

    Hi There,
    I’m trying to order my results page by a number field in ACF.

    Here’s where I am at with no luck – any suggestions welcome!

    <?php
    /**
     * Search & Filter Pro 
     *
     * Sample Results Template
     * 
     * @package   Search_Filter
     * @author    Ross Morsali
     * @link      https://searchandfilter.com
     * @copyright 2018 Search & Filter
     * 
     * Note: these templates are not full page templates, rather 
     * just an encaspulation of the your results loop which should
     * be inserted in to other pages by using a shortcode - think 
     * of it as a template part
     * 
     * This template is an absolute base example showing you what
     * you can do, for more customisation see the WordPress docs 
     * and using template tags - 
     * 
     * http://codex.wordpress.org/Template_Tags
     *
     */
     
    $query = get_posts(array(
    	'meta_key'			=> 'DST-carat',
    	'orderby'			=> 'meta_value',
    	'order'				=> 'DESC'
    )); 
    
    if ( $query->have_posts() )
    {
    	?>
    
    	
    <div class="row">
        <div class="col-12">
            
            <table>
                <tbody>
                    
    
            	
    	<?php
    	while ($query->have_posts())
    	{
    		$query->the_post();
    		
    		?>
    		
    		
            <tr>
                <td><?php the_field('DST-shape'); ?></td>
                <td><?php the_field('DST-carat'); ?></td>
                <td><?php the_field('DST-colour'); ?></td>
                <td><?php the_field('DST-clarity'); ?></td>
                <td><?php the_field('DST-fluorescence'); ?></td>
                <td><?php the_field('DST-lab'); ?></td>
                <td><a href="https://www.gia.edu/report-check?reportno=<?php the_field('DST-certificate'); ?>" target="_blank">GIA Certificate</a></td>
                <td>Make an enquiry</td>
            </tr>
    		
    		<?php
    	}
    	?>
    
                </tbody>
        </table>
    
    	</div>
    </div>	
    	
    	
    	Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
    	
    	<div class="pagination">
    		
    		<div class="nav-previous"><?php next_posts_link( 'Older posts', $query->max_num_pages ); ?></div>
    		<div class="nav-next"><?php previous_posts_link( 'Newer posts' ); ?></div>
    		<?php
    			/* example code for using the wp_pagenavi plugin */
    			if (function_exists('wp_pagenavi'))
    			{
    				echo "<br />";
    				wp_pagenavi( array( 'query' => $query ) );
    			}
    		?>
    	</div>
    	<?php
    }
    else
    {
    	echo "No Results Found";
    }
    ?>
    Anonymous
    #228403

    EDIT:

    $query = new WP_Query(array(
    	'post_type'			=> 'diamond_search_tool',
    	'posts_per_page'	=> -1,
    	'meta_key'			=> 'DST-carat',
    	'orderby'			=> 'meta_value',
    	'order'				=> 'ASC'
    ));

    Displays content in intended but disables the filter.
    Using Shortcodes.

    Trevor
    #228434

    You should not be adding the query into the results.php file.

    Instead use the Posts tab in the form design, and that has a sort option. Use Meta Value, select the ACF key name (make sure it DOES NOT start with an underscore) and then sort direction. See here an example:

    https://www.screencast.com/t/rVKgFNJl7FD

    Note, when you sort in this way, any post that has no value set will be excluded from the results.

    Anonymous
    #228436

    Awesome, thanks for clearing that up for me. Great support + great plugin 🙂

    Trevor
    #228440

    Thanks for getting back to me. I will close this thread for now.

Viewing 5 posts - 1 through 5 (of 5 total)