首页 文章

不同post_types上的WP_Query,每个post_type都有附加条件

提问于
浏览
1

简而言之,我有以下情况:由具有自定义帖子类型的用户发布的产品 . 称他们为“广告” . 广告有一个1-1链接到WooCommerce产品 . 除此之外,我还有供应商项目的WooCommerce产品(它们由多个供应商插件添加),并与meta_key“_vendor_product”设置为1的其他post_type产品区分开来 .

到目前为止,我做了2个查询并添加了它们:

$ads = new WP_Query(
    array(
        'post_type' => 'ads',
        'paged' => $paged,
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'product_id',
                'value' => $product_ids,
                'compare' => 'IN'
            )
        )
    )
);
$shop_items = new WP_Query(
    array(
        'post_type' => 'product',
        'paged' => $paged,
        'post__in' => $product_ids,
        'meta_query' => array(
            array(
                'key' => '_vendor_product',
                'value' => 1,
                'compare' => '='
            )
        )
    )
 );
}

// Include shop items in the stream of products
$wp_query = new WP_Query(array('paged' => $paged));
$wp_query->posts = array_merge( $ads->posts, $shop_items->posts );
$wp_query->post_count = $ads->post_count + $shop_items->post_count;

但是我需要在一个WP_Query中使用它们,否则我的动作/过滤器/分页似乎会破坏 . 我知道我可以通过 'post_type' => array( 'ads', 'product' ) 查询不同的post_types,但 _vendor_product = 1 的条件仅适用于post_type product 的项目,而不适用于 ads .

基本上我需要的是什么

SELECT * FROM wp_posts, wp_postmeta 
WHERE (post_type = 'ads') 
OR 
(post_type = 'product' AND meta_key = '_vendor_product' AND meta_value = 1) 
AND 
wp_posts.ID = wp_postmeta.post_id

是否有可能在WP_Query的范围内实现这一目标?

2 回答

  • 0

    我怀疑你可以直接在一个查询中实现它(但是有人可能会提出一个解决方案) . 但是,我建议一个简单的解决方法来解决这个问题 - 找出帖子ID并从中进行单一查询:

    global $wpdb;
    
    $all_needed = $wpdb->get_results('SELECT wp_posts.id FROM wp_posts, wp_postmeta 
    WHERE (post_type = 'ads') 
    OR 
    (post_type = 'product' AND meta_key = '_vendor_product' AND meta_value = 1) 
    AND 
    wp_posts.ID = wp_postmeta.post_id');
    
    // extract_ids is a pseudo-code function - check what the result actually is and make an array of post_ids
    $post_ids = extract_ids($all_needed)
    
    // now you have your single query
    $query = new WP_Query(array('post__in'=>$post_ids));
    
    // proceed ....
    
  • 0

    我基本上以Blackbam建议的方式实现了我的结果,尽管没有直接涉及SQL .

    如果有人发现这个我会发布我的解决方案,因为它让我有点头疼,弄清楚为什么我的结果在WP_Query上使用get_posts时限制为10(解决方案是WP_Query中的 'nopaging' ) .

    $ps_ads = new WP_Query(
        array(
            'post_type' => 'ads',
            'fields' => 'ids',
            'nopaging' => true,
            'post_status' => 'publish'
        )
    );
    $shop_items = new WP_Query(
        array(
            'post_type' => 'product',
            'fields' => 'ids',
            'nopaging' => true,
            'post__in' => $product_ids,
            'meta_query' => array(
                array(
                    'key' => '_vendor_product',
                    'value' => 1,
                    'compare' => '='
                )
            )
        )
     );
    }
    
    // Sum up all IDs
    
    $ad_ids = $ps_ads->get_posts();
    $shop_items_ids = $shop_items->get_posts();
    $ids = array_merge($ad_ids, $shop_items_ids);
    
    $wp_query = new WP_Query(
        array(
            'post_type' => array( 'ads' , 'product' ),
            'paged' => $paged,
            'post__in' => $ids
        )
    );
    

    希望能帮助到你 .

相关问题