简体   繁体   中英

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

$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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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