• Can anyone help me accomplish the following?

    I have a meta key ‘total_gb’ that is attached to a custom post type ‘data’. I can retrieve and sum the values using the following query:

    function get_meta_values( $key = 'total_gb', $type = 'data', $status = 'publish' ) {
        global $wpdb;
        if( empty( $key ) )
            return;
        $r = $wpdb->get_col( $wpdb->prepare( "
            SELECT pm.meta_value FROM {$wpdb->postmeta} pm
            LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
            WHERE pm.meta_key = '%s'
            AND p.post_status = '%s'
            AND p.post_type = '%s'
        ", $key, $status, $type ) );
        return $r;
    }
    
    $my_totalgb = get_meta_values( 'total_gb' );
    if( !empty( $my_totalgb ) ) {
        $meta_counts = array();
        foreach( $my_totalgb as $meta_value )
            $meta_counts[$meta_value] = ( isset( $meta_counts[$meta_value] ) ) ? $meta_counts[$meta_value] + 1 : 1;
    }
    
    echo "<br/>Total Data = " . array_sum($my_var) . "GB \n";
    ?>

    This totals all of them base on the query criteria. Phase 1 complete.

    Next, I can perform a multiple taxonomy “search” and get all the ‘data’ post types based on the criteria of the search. Lets say taxonomy x and y. This gives me a list of ‘data’ posts in both x and y taxonomies on the search.php page. Is it possible to use the post ids of those results and total the ‘total_gb’ from only them on the search results page. I guess it would have to do this on the fly, taking the ids from the results and using them as a variable for an added ‘post_id’ in the query?

    I’m thinking that adding this would help to that end.
    AND p.ID = '$search_ids'

    but how do I get the search result IDs in the first place, and how can I get them into a form useful variable for the query?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Your proposed method would work, or you could just expand your script example to join in the taxonomies and add criteria so that the query returns only the meta values associated with x and y, instead of, or in addition to, your current criteria.

    This way your code doesn’t deal with IDs directly, but only as part of the query in order to join in the tables you need. Let SQL deal with the ID forms, that’s what it’s good at.

    Thread Starter John

    (@jsing)

    I am not sure I follow exactly. I am using Taxonomy Picker to let the user build the query. What I don’t know is how to use that custom query and use it to return the custom field values.

    So the taxonomies would vary with each search so I don’t think I can hard code them in. That is why I thought the ID’s of the posts on the page would be good.

    As stated I have little php or sql knowledge so I don’t know how to list the ID’s from the results much less use them as a variable in a query.

    I also don’t know how to go about joining the taxonomy table etc.

    Any help would be great! Thanks for your time.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘List post ids for all results of search’ is closed to new replies.