0

I have tried searching everywhere for this and although there are some discussions about adding the total to the frontend, there is nothing about the admin side.

I have a custom post type that has posts and one fo the fields of those posts is a price. I would like for the statuses (all, published, concept...) to show the relevant sum total of that field. Just like the statuses show the total number of posts in perentheses.

Is there a smart way to do this?

0
0

Get all of the post ids from the specific post type, loop through them and simply add the custom fields together and display the sum below the post type title on edit.php (Notice the add_filter first parameter, you need to change the products part to your post type name)

add_filter('views_edit-products','products_price_sum');
function products_price_sum($views) {

    $args = array(
        'post_type' => 'products',
        'posts_per_page' => -1,
        'post_status' => 'any',
        'fields' => 'ids'
    );
    $postslist = get_posts( $args );
    $sum_total = 0;

    foreach ($postslist as $post) {
        $price = get_post_meta( $post, 'price', true );
        $sum_total = $sum_total+$price;
    }

    //This is the sum of the price field
    $views['price_sum'] = '<strong>Price Sum:</strong> '.$sum_total;

    return $views;

}           
10
  • I have been trying a similar code, but yours is much more simple. Thank you. – Fid Mar 23 '14 at 20:49
  • What would be the easiest way to add the output to the edit.php without messing with the core? – Fid Mar 23 '14 at 20:51
  • Theres a couple of available filters/actions, where exactly would you like to display this value? – passatgt Mar 23 '14 at 21:18
  • Ie. above the search field, horizontally aligned right from the post type title is a blank spot, that would be awesome. – Fid Mar 23 '14 at 21:21
  • The best i could find is the views_{$this->screen->id} filter, which is the top left post counter below the post type title, i updated the answer – passatgt Mar 23 '14 at 21:39
0

Here is the final version that changes based on the clicked status or taxonomy.

Future readers will need to change:

custom_post_name - the slug name of your custom post

custom_taxonomy_name - if you have one, otherwise just 'taxonomy'

_custom_field_id - the meta id of the field you want to sum up

The reason for the custom taxonomy name is that I have a column with prices and a column with a "category". When you click on a status, you will see the price of all the custom posts in that status, but I wanted to further filter the price based on the "category". When you click on it, it shows only those specific posts and the total price changes to only that custom taxonomy.

Thanks to everyone for their help!

foreach( array( 'custom_post_name' ) as $hook )
add_filter( "views_edit-$hook",'products_price_sum_dpf');
function products_price_sum_dpf($views) {
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
$slug = $term->slug;

if ($_GET['post_status'] != NULL) { 
    $args = array(
        'post_type' => 'custom_post_name',
        'posts_per_page' => -1,
        'post_status' => $_GET['post_status'],
        'fields' => 'ids',
        'custom_taxonomy_name' => $slug
);
} else {
    $args = array(
        'post_type' => 'custom_post_name',
        'posts_per_page' => -1,
        'post_status' => 'test',
        'custom_taxonomy_name' => $slug,
        'fields' => 'ids'   
    );
};  


$postslist = get_posts( $args );
$sum_total = 0;

foreach ($postslist as $post) {
    $price = get_post_meta( $post, '_custom_field_id', true );
    $sum_total = $sum_total+$price;
}

//This is the sum of the price 
if ($_GET['post_status'] == NULL && $slug == NULL) {
    echo '';
} else {
$views['price_sum'] = '<strong style="color:#ff0000">Total: '.$sum_total.'</strong>';
}
return $views;

}           

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.