Support

Account

Home Forums Backend Issues (wp-admin) Sorting search results by ACF date fields

Solved

Sorting search results by ACF date fields

  • Hello
    I have two custom post types (kurse, infoabend) with ACF date fields. The query of these posts sorted by date works fine.

    
    $today = date ('Ymd');
    $args = array(
     'post_type'      => array( 'kurse', 'infoabend' ),
     'post_status'    => array( 'publish', 'future' ),
     'posts_per_page' => $posts_per_page,
     'paged'          => $paged,
     'meta_query'     => array(
            'relation'         => 'OR',
    	'kurs_clause'      => array(
    	     'key'     => 'kurs_kursdaten_0_datum',
    	     'compare' => '>=',
    	     'value'   => $today,
              ),
    	'infoabend_clause' => array(
    	     'key'     => 'datum',
    	     'compare' => '>=',
    	     'value'   => $today,
    	  ),
    	),
    	'orderby'        => array(
    		'kurs_clause'      => $order,
    		'infoabend_clause' => $order,
    	),
    ); 

    I added ACF fields to the search and limited search to these two post types. To be able to sort the search results by date, I added the meta query I used in the first query.

    add_action( 'pre_get_posts', __NAMESPACE__ . '\\d_edit_wp_query' );
    
    function d_edit_wp_query( $query ) {
      if ( ! is_admin() && $query->is_main_query() && $query->is_search ) {
    
         if( !empty( $query->query['orderby'] ) && 'date' === $query->query['orderby'] {
                $current_meta = ( empty ( $query->get('meta_query' ) ) )
    		? []
    		: $query->get( 'meta_query' );
    
                $today = date ('Ymd');
    
                $custom_meta = [
    		'relation'         => 'OR',
    		'kurs_clause'      => array(
    		    'key'       => 'kurs_kursdaten_0_datum',
    		    'compare'   => '>=',
    		    'value'     => $today,
    		    'meta_type'	=> 'DATE',
    		),
    		'infoabend_clause' => array(
    		    'key'       => 'datum',
    		    'compare'   => '>=',
    		    'value'     => $today,
    		    'meta_type'	=> 'DATE',
    		),
    		'orderby' => array(
    		  'kurs_clause'      => $query->query['order'],
    		  'infoabend_clause' => $query->query['order'],
    		)
    	];
    	$meta_query = $current_meta[] = $custom_meta;
         }
      }
    }

    Unfortunately, the sorting by date does not work correctly. I would be grateful for any tips and ideas on how to solve the problem.

  • You have 2 problems

    The first is an error in your code, this

    
    $query->is_search
    

    should be this

    
    $query->is_search()
    

    The second thing is that ordering by several fields is not going to work the way I suspect you think it will or want it do. The second field will be order within the order or the first. An example:

    Let’s say that you have two fields

    1. State
    2. County

    and you order the posts first by state and then by county. It will order all of the posts alphabetically by state and then by county within each state.

    • State
      • county
      • county
      • county
      • county
      • county
      • county
    • State
      • county
      • county
      • county
      • county
      • county
      • county
  • Hello John

    Many thanks for the advice. I have corrected the spelling mistake in is_search.

    The explanation regarding the sorting explains exactly what I see in the output of the search results after sorting by date.

    What I still don’t understand is that the sorting works when querying the two cpts on an overview page (first code section) but not in the search.

    Can I rewrite the search query so that sorting by date works or would ACF fields have to be named the same in order to enable sorting with $query->set('orderby', 'meta_value'); $query->set('meta_key', 'date_field_name');?

  • Yes, the field names on the two post types would have to be the same to get the posts intermingled.

    In order to use 2 different field names the fields must have some relationship to each other.

    The only other options is would be to do two different queries, add the value of the acf field as a property of each post object, merge the results of both and then manually sort them in one of the php array sorting functions, likely with usort().

  • Hello John
    Thank you very much for the explanations and tips, that helps me a lot. I now need to think about which option is best for me to implement.

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

You must be logged in to reply to this topic.