wpgenie icon

prevent displaying ended auctions if option to show PAST auctions is enabled for shortcodes

wpgenie | PRO | 10/01/20 08:35:29 AM UTC (Edited) | 0 ⭐ | 2920 👁️ | Never ⏰ | []
PHP |

680 B

|

None

|

0 👍

/

0 👎

/* 
- enabled option to show PAST auctions
- prevent showing PAST auctions unless you are on the shop page
- useful for shortcodes or widgets to prevent displaying ended auctions if option to show PAST auctions is enabled
- code snippet should go to your custom plugin or functions.php file
 
code by Magnus
*/
 
add_filter( 'woocommerce_product_query_meta_query', 'show_only_products_with_specific_metakey', 10, 2 );
function show_only_products_with_specific_metakey( $meta_query, $query ) {
 
  // Only on shop pages
  if( ! is_shop() ) return $meta_query;
 
  $meta_query[] = array(
    'key'   => '_auction_closed',
    'compare' => 'NOT EXISTS'
  );
 
  return $meta_query;
}

Comments