List post ids for all results of search
-
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?
- The topic ‘List post ids for all results of search’ is closed to new replies.