I added an additional activity feed to my profile page that queries for a linked user and returns their activity. The problem is, I want to write this over to a function as there will often be other areas of the site that need to use this same query.
Here is the user query. It’s just a comparison query to find and return the linked user’s ID.
global $bp, $wpdb;
$myid = $bp->loggedin_user->id;
$ourlink = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'linkid' AND user_id = '$myid'");
$partnerid = $wpdb->get_var("SELECT user_id from $wpdb->usermeta where meta_key='linkid' AND meta_value = '$ourlink' AND user_id != '$myid'");
$partner = '&user_id=' . $partnerid;
Which is then used in the activity loop.
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . $partner ) ) : ?>
However, rewriting this as a function and then calling that function right before the loop, though it will still return the ID, it no longer works for the sake of the activity loop.
Function: (in WP functions.php file)
function sw_partner_id() {
global $bp, $wpdb;
$myid = $bp->loggedin_user->id;
$ourlink = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'linkid' AND user_id = '$myid'");
$partnerid = $wpdb->get_var("SELECT user_id from $wpdb->usermeta where meta_key='linkid' AND meta_value = '$ourlink' AND user_id != '$myid'");
$partner = '&user_id=' . $partnerid;
return $partner;
}
Calling the function before the activity loop
<?php sw_partner_id(); ?>
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . $partner ) ) : ?>
If you want to call a function inside your functions.php file use hooks.Please notice
global bp
in your code since you forgot to add a $ sign.Example:
After that try calling the function.
I hope that helps.