ControlledChaos
5/7/2015 - 8:27 AM

Retrieve a value from database with $wpdb

Retrieve a value from database with $wpdb

<?php

global $wpdb;

$result = "";
$query = ("SELECT
  sum($wpdb->postmeta.meta_value)
  FROM $wpdb->posts
  INNER JOIN $wpdb->postmeta
  ON $wpdb->posts.id = $wpdb->postmeta.post_id
  WHERE post_status = 'publish' && post_type = 'cases'
  GROUP BY meta_key
  LIKE '%negative%'
  ");
	
$result = $wpdb->get_results( $query, ARRAY_N );

echo $result[1][0];

?>

Retrieve a value from database with $wpdb

WordPress Snippet