Advertisement
Guest User

Dominykas

a guest
Sep 3rd, 2017
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. /**
  2.  * Get products from events category and sort by woodate.
  3.  *
  4.  * Alter the main loop
  5.  *
  6.  * @uses pre_get_posts hook
  7. */
  8. function theme_modify_the_main_query( $query ) {
  9.     // Order by: woodate
  10.     if ( isset( $_GET['orderby'] ) && $_GET['orderby'] == 'woodate' ) {
  11.         // Modify the main query
  12.         if( ! is_admin() && $query->is_main_query() ) {
  13.  
  14.             // Get from Events category
  15.             $tax_query = array(
  16.                 array(
  17.                     'taxonomy' => 'product_cat',
  18.                     'field' => 'slug',
  19.                     'terms' => 'events' // Events category (term) slug
  20.                 )
  21.             );
  22.  
  23.             $query->set( 'tax_query', $tax_query );
  24.  
  25.             $query->set( 'meta_key', 'woodate' );
  26.             $query->set( 'orderby', 'meta_value_num' );
  27.             $query->set( 'order', 'DESC' );
  28.         }
  29.     }
  30. }
  31. add_action( 'pre_get_posts', 'theme_modify_the_main_query' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement