Guest User

Dominykas

a guest
Sep 3rd, 2017
480
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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' );
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×