• Resolved rudtek2

    (@rudtek2)


    How can I set the entire catalog (on the shop page and on category pages) by a custom field value?

    My custom field is created by ACF and is called event_date.

    Here’s what I tried:

    function products_pre_get_posts( $query ) {
    	
    	// do not modify queries in the admin
    	if ( is_admin() || !$query->is_main_query() )
    		
    		return $query;
    	
    	// only modify queries for 'event' post type
    	if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'product' ) {
    		
    		$query->set('orderby', 'meta_value');	
    		$query->set('meta_key', 'event_date');	 
    		$query->set('order', 'DESC'); 
    		
    	}
    	
    	// return
    	return $query;
    
    }
    
    add_action('pre_get_posts', 'products_pre_get_posts');

    This sorts the shop page but not the category pages.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey @rudtek2,

    I believe this will be false for a product category which will keep the rest of this from running.

    
    isset($query->query_vars['post_type'])
    

    Perhaps something like this instead:

    
    if ( is_shop() || is_product_category() ) {
    

    That would cover the main product archive as well as product categories.

    Hi @rudtek2,

    It’s been a while since we heard from you, so I’m marking this thread resolved. Hopefully, you’ve been able to resolve this, but if you haven’t, please open up a new topic and we’ll be happy to help out.

    Cheers

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘sort product catalog’ is closed to new replies.