Quantcast
Channel: Ruben Sargsyan » custom fields
Viewing all articles
Browse latest Browse all 2

Get values of a custom field belonging to the category or tag posts in WordPress

0
0

This is an example how to get the values of a custom field belonging to the category or tag posts in WordPress:

<?php
function get_custom_field_values($custom_field,$term_id){
    global $wpdb;
 
    $query = "SELECT ".$wpdb->prefix."postmeta.meta_value FROM ".$wpdb->prefix."postmeta JOIN ".$wpdb->prefix."term_relationships ON ".$wpdb->prefix."term_relationships.object_id = ".$wpdb->prefix."postmeta.post_id JOIN ".$wpdb->prefix."term_taxonomy ON ".$wpdb->prefix."term_relationships.term_taxonomy_id = ".$wpdb->prefix."term_taxonomy.term_taxonomy_id AND ".$wpdb->prefix."term_taxonomy.term_id='".$term_id."' WHERE meta_key = '".$custom_field."' GROUP BY ".$wpdb->prefix."postmeta.meta_value;";
 
    return $wpdb->get_results($query);
}
 
 
$custom_field = "Brand"; //Custom field name
$term_id = 7; //Category or tag ID
 
$custom_field_values = get_custom_field_values($custom_field,$term_id);
?>

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images