0

我需要自定义查询来显示循环之外的帖子数。

结果需要过滤:

Taxanomy = ad_cat
Term_id = 32
Meta_key = cp_type
Meta_value = sale
Post-type = ad_listings

我有这个sql:

$query = "
SELECT *
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->terms.term_id = '32'
AND $wpdb->postmeta.meta_key = 'cp_tips' 
AND $wpdb->postmeta.meta_value = 'Pārdod' 
AND $wpdb->posts.post_status = 'publish' 
AND $wpdb->posts.post_type = 'ad_listing'
";

但在这个查询中,我不知道如何添加术语:

术语 sql:

SELECT ID as PID FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'ad_cat'
AND $wpdb->term_taxonomy.term_id = 32

如何把所有东西放在一起?

4

2 回答 2

0

I recommend wp_query
Not tested especially the taxonomy part, but itsShould work, or at least get you started.
The big advantage of wp_query is that you can use the loop on it

<?php
$query = array (
   'post_type' => 'ad_listing',
   'post_status' => 'publish' ,

   //que custom field
    'meta_query' => array(
        array(
            'key' => 'cp_tips',
            'value' => 'Pardod'
        )
    ),

    //term 32
    'tax_query' => array(
        array(
            'taxonomy' => 'ad_cat',
            'field' => '32',
                    'terms' => array( 'action', 'commedy' )
        )
    )
);

$ad_list = new WP_Query($query);

Let me know how it goes

于 2012-04-10T20:16:23.280 回答
0

您可以使用WP_Query来完成。需要用到 tax_query 和 meta_query 的部分,然后可以从属性 found_posts 中获取计数。检查法典,它有示例和更好地解释使用 WP_Query 的所有选项

于 2017-06-29T09:41:33.647 回答