Support » Plugins » Hacks » Trying to Count Number of rows of a custom field

  • I am using the code to do this. Instead of the variable in the sql string, if I use the actual value, its working. I mean instead of $country I use “USA” its working. But when I use the variable, its not while at the same time $country is outputting USA.

    Can someone help me???? Would greatly appreciate it

    [please mark any code using the code button – http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

    global $wpdb;
    $countries = tgt_get_countries();
    foreach ($countries as $country) {
    $sqlquery="SELECT COUNT(*) FROM ($wpdb->postmeta WHERE meta_value = '".$country."' AND meta_key = 'tgt_job_country')";
    $testcount = $wpdb->get_var($sqlquery);
    echo "
    <li>".$country."(".$testcount.")";}
Viewing 2 replies - 1 through 2 (of 2 total)
  • global $wpdb;
    $countries = tgt_get_countries();
    foreach ($countries as $country) {
    $sqlquery="SELECT COUNT(*) FROM ($wpdb->postmeta WHERE meta_value = '$country' AND meta_key = 'tgt_job_country')";
    $testcount = $wpdb->get_var($sqlquery);
    echo "
    <li>".$country."(".$testcount.")";}

    The above code also doesnt work

    corrected it just the additional brackets was the problem….

    <?php
    		global $wpdb;
    		$countries = tgt_get_countries();
    		foreach ($countries as $country) {
    		$testcount = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->postmeta WHERE meta_value = '$country' AND meta_key = 'tgt_job_country'");
    		if ($testcount !=0)
    		echo "<li>".$country."(".$testcount.")";}
    	?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Trying to Count Number of rows of a custom field’ is closed to new replies.