简体   繁体   English

Wordpresss - WP_query 计数

[英]Wordpresss - WP_query count

I need to count how many times my date recorded in the meta key: metakey_AMC_data , in format (dmY) it is contained in the database by comparing it with the current date我需要计算我的日期在元键中记录了多少次: metakey_AMC_data ,格式为(dmY),通过将其与当前日期进行比较,它包含在数据库中

$mostra_data_corrente = date('d-m-Y');

$query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta 
         WHERE (meta_key = 'metakey_AMC_data' 
         AND meta_value = '$mostra_data_corrente')");
         $conta_risultati =  count($query);

and this I can do perfectly.but now my need is to execute the first query by linking another AND, and specify when the term slug is equal to the category of the event (terms taxonomy), obviously the query is incorrect我可以完美地做到这一点。但现在我需要通过链接另一个 AND 来执行第一个查询,并指定术语 slug 何时等于事件的类别(术语分类),显然查询不正确

SELECT * FROM {$wpdb->prefix}postmeta 
WHERE (meta_key = 'metakey_AMC_data' 
AND meta_value = '$mostra_data_corrente') 
AND(slug = 'aperitivi') "

how can i do this?我怎样才能做到这一点?

You can get that count as well.你也可以得到这个数。 You need to modify query (code) like follow:您需要修改查询(代码),如下所示:

$qry = array(
    'post_type' => 'post', // mention your post type to narrow down searching through postmeta table
    'meta_query' => array(
        array(
            'meta_key' => 'metakey_AMC_data',
            'meta_value' => $mostra_data_corrente,
            'compare' => '='
        )
    ),
    'tax_query' => array(
        array(
            'taxonomy' => 'nameoftaxonomy', // Write the name of taxonomy that you have assinged while you created a CPT (custom post type)
            'field'    => 'slug',
            'terms'    => 'aperitivi',
        )
    )
)

$the_query = WP_Query($qry);
echo $the_query->post_count;

You have to make some necessary changes in above code to suite your requirements.您必须对上述代码进行一些必要的更改以适应您的要求。 I've added comment where you have to do changes.我已经在您必须进行更改的地方添加了评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM