WordPress教程:WooCommerce返回特色产品ID

返回特色产品ID

function woo_get_featured_product_ids() {
  // Load from cache
  $featured_product_ids = get_transient( 'wc_featured_products' );
 
  // Valid cache found
  if ( false !== $featured_product_ids )
    return $featured_product_ids;
 
  $featured = get_posts( array(
    'post_type'      => array( 'product', 'product_variation' ),
    'posts_per_page' => -1,
    'post_status'    => 'publish',
    'meta_query'     => array(
      array(
        'key'       => '_visibility',
        'value'     => array('catalog', 'visible'),
        'compare'   => 'IN'
      ),
      array(
        'key'   => '_featured',
        'value' => 'yes'
      )
    ),
    'fields' => 'id=>parent'
  ) );
 
  $product_ids = array_keys( $featured );
  $parent_ids  = array_values( $featured );
  $featured_product_ids = array_unique( array_merge( $product_ids, $parent_ids ) );
 
  set_transient( 'wc_featured_products', $featured_product_ids );
 
  return $featured_product_ids;
}

在哪里添加此代码?

将PHP代码放在主题或子主题functions.php文件的底部。

小朱博客

View posts by 小朱博客
小朱个人博客,生活网络分享,外贸建站,谷歌seo操作分享,B2B优化排名操作分享,资源分享,软件分享,生活分享。https://www.zhudc.com/

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

Scroll to top