最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get all of a user's posts (custom post type) then get cumulative value of a specific meta_key value from those posts

programmeradmin4浏览0评论

I'm trying to add property values for each user on a real estate website.

I have a custom post type "properties." I'd like to take the "listing_price" from all of them and add them up to show how much that agent has for active listings.

Really don't even know where to start. I see threads to add all custom post type meta values but not for a specific user.

thank you

I'm trying to add property values for each user on a real estate website.

I have a custom post type "properties." I'd like to take the "listing_price" from all of them and add them up to show how much that agent has for active listings.

Really don't even know where to start. I see threads to add all custom post type meta values but not for a specific user.

thank you

Share Improve this question asked Sep 9, 2019 at 23:04 user109430user109430 193 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0
    add_shortcode( 'show_total_listing_price', 'show_total_listing_price' );

    function show_total_listing_price ( $atts ) {
    global $post;


    $args = array(

    'author' => get_current_user_id(),
    'post_type' => 'POST_TYPE_GOES_HERE',
    'posts_per_page' => -1,
    'post_status' => 'publish',

    );

    $query = new WP_Query( $args );
    $total = 0;
    if( $query->have_posts() ){
    while( $query->have_posts() ){
    $query->the_post();
    $price = get_post_meta( $post->ID, 'PLACE_META_KEY_HERE', true );
    $total += $price;
    }
    }
    wp_reset_postdata();
    echo $total;

    }

This code works! Just now having an issue because I have 2 types of posts with this custom post type. There's another meta_key called 'check_status' which shows active or pending properties. The above code combines both of these.

Need to modify it so that chech_status='1' only gets counted.

<?php 
    $args = array(
    'post_type' => 'corona',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'ASC'
    );

    $query = new WP_Query( $args );
    $total = 0;
    if( $query->have_posts() ){
    while( $query->have_posts() ){
    $query->the_post();
    $price = get_post_meta( $post->ID, '_country_total_case', false );

    foreach ( $price as $item) :
        $ttt += $item;
    echo "<p>$ttt</p>";
    endforeach;    
    }
    }
?>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论