wp query - Count post meta entries

admin2025-04-20  0

I have to count how many times my custom post type date enclosed in my metakey:metakey_AMC_data, it is the same as the current date and belongs to a specific taxonomy

I can't get this to work:

try 1:with this, i have 4 result but not the truth because i have only one

<?php
$mostra_data_corrente = date('d-m-Y');
$args = array(
    'post_type' => 'eventi-suite',
    'meta_query' => array(
        array(
            'key'       => 'metakey_AMC_data',
            'type'              => 'DATE',
            'meta_value'        => $mostra_data_corrente,
        )
    ),
    'tax_query' => array(
        array(
            'taxonomy'  => 'categoria',
            'field'     => 'slug',
            'terms'     => $queried_object,
        )
    ),
    'post_status'   => 'publish',

);
$query = new WP_Query( $args );
$conta_risultati =  count($args);
echo $conta_risultati;
?>

try 2: I can't determine the part of the taxonomy, and doing it for id is madness

    global $post;
    $show_id = $post->ID;
    $mostra_data_corrente = date('d-m-Y');
    global $wpdb;
    $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta, {$wpdb->prefix}term_relationships
                                        WHERE (meta_key = 'metakey_AMC_data' 
                                        AND meta_value = '$mostra_data_corrente'
                                        AND object_id = $show_id) ");
    $conta_risultati =  count($query);

I would simply like to count how many times the date of my meta key in (d-m-y) format is present inside the db, only posts from a given taxonomy

I have to count how many times my custom post type date enclosed in my metakey:metakey_AMC_data, it is the same as the current date and belongs to a specific taxonomy

I can't get this to work:

try 1:with this, i have 4 result but not the truth because i have only one

<?php
$mostra_data_corrente = date('d-m-Y');
$args = array(
    'post_type' => 'eventi-suite',
    'meta_query' => array(
        array(
            'key'       => 'metakey_AMC_data',
            'type'              => 'DATE',
            'meta_value'        => $mostra_data_corrente,
        )
    ),
    'tax_query' => array(
        array(
            'taxonomy'  => 'categoria',
            'field'     => 'slug',
            'terms'     => $queried_object,
        )
    ),
    'post_status'   => 'publish',

);
$query = new WP_Query( $args );
$conta_risultati =  count($args);
echo $conta_risultati;
?>

try 2: I can't determine the part of the taxonomy, and doing it for id is madness

    global $post;
    $show_id = $post->ID;
    $mostra_data_corrente = date('d-m-Y');
    global $wpdb;
    $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta, {$wpdb->prefix}term_relationships
                                        WHERE (meta_key = 'metakey_AMC_data' 
                                        AND meta_value = '$mostra_data_corrente'
                                        AND object_id = $show_id) ");
    $conta_risultati =  count($query);

I would simply like to count how many times the date of my meta key in (d-m-y) format is present inside the db, only posts from a given taxonomy

Share Improve this question edited Oct 1, 2019 at 16:29 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Oct 1, 2019 at 15:46 dcerdcer 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

WP_Query has a property that returns the no of posts that match your query.

You can print it as follow in your first try:

$query = new WP_Query( $args );

echo $query->post_count;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745114938a285807.html

最新回复(0)