自定义 Woocommerce 循环查询

Customizing Woocommerce Loop Query

我想在网站的两个不同部分显示我的 woocommerce 产品,即商店页面和我使用自定义 Post UI 插件创建的存档页面,称为艺术家。

使用 Advanced Custom Fields 插件,我创建了一个字段,将创建的每个产品附加到单个艺术家。现在的计划是,在每个艺术家页面上,为艺术家拉产品。

这就是我目前所拥有的。我连接到 woocommerce_product_query,检查我是否在单个艺术家页面上并执行我的元查询:

function artist_products( $query ) 
{
    if( is_singular('artists') )
    {
        $meta_query = ( is_array( $query->get('meta_query') ) ) ? $query->get('meta_query') : [];

        $meta_query[] = array(
                'key' => '_fld_select_artist',
                'value' => get_the_ID(),
         );
    
        $query->set( 'meta_query', $meta_query );
    }
}

add_action( 'woocommerce_product_query', 'artist_products' );

这是行不通的。当我访问我的单人艺术家页面时,出现 500 服务器错误。我缺少什么?

woocommerce_product_query 由于某种原因破坏了我的网站所以我切换到 pre_get_posts:

function artist_products( $query ) 
{
    if ( $query->get('post_type') == 'nav_menu_item' )
    {
        return $query;
    }

    if( ! is_admin() && is_singular('artists') )
    {
        $meta_query = ( is_array( $query->get('meta_query') ) ) ? $query->get('meta_query') : [];

        $meta_query[] = array(
            'key' => '_fld_select_artist',
            'value' => get_the_ID(),
            'compare'   => '=',
        );

        $query->set( 'meta_query', $meta_query );
    }
}

add_action( 'pre_get_posts', 'artist_products' );