Hmmm, I’m wondering if the source code of https://erwin.terong.com/2005/09/24/wp-plugin-related-posts-link/ holds any answers for my quest… (puts on ‘serious’ hat)
I’ve found 2 functions so far that are giving me clues… not sure what to do to them next though:
function get_all_meta($key, $single = false)
{
global $wpdb, $post_meta_cache;
$metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '$key'", ARRAY_N);
$values = array();
if ( $metalist ) {
foreach ($metalist as $metarow) {
$values[] = $metarow[0];
}
}
if ( $single ) {
if ( count($values) ) {
$return = maybe_unserialize( $values[0] );
} else {
return '';
}
} else {
$return = $values;
}
return maybe_unserialize($return);
}
?>
/////////////////////////////////////
/////////////////////////////////////
/////////////////////////////////////
function get_related_ids($post_id) {
global $wpdb;
$sql = "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE meta_key in('related_post_name', 'related_id') AND post_id=$post_id";
$searches = $wpdb->get_results($sql);
if($searches) {
$post_name = array();
$post_id = array();
foreach($searches as $s) {
if($s->meta_key=='related_post_name') {
$arr = explode(',', $s->meta_value);
foreach($arr as $a) $post_name[] = ("'".trim($a)."'");
}
elseif($s->meta_key=='related_id') {
$arr = explode(',', $s->meta_value);
foreach($arr as $a) $post_id[] = ("'".trim($a)."'");
}
}
$text_post_name = implode(', ', $post_name);
$text_post_id = implode(', ', $post_id);
$sql = "SELECT DISTINCT post_title, ID FROM $wpdb->posts WHERE ";
$arr_sql = array();
if($post_name) $arr_sql[] = "post_name in($text_post_name)";
if($post_id) $arr_sql[] = "ID in($text_post_id)";
$sql .= implode(" OR ", $arr_sql);
$sql .= " ORDER BY ID DESC";
$searches = $wpdb->get_results($sql);
return($searches);
}
else return array();
}