Buddypress custom activity loop and query function

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.

Read More
    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 ) ) : ?>

Error Update:
enter image description here
enter image description here

Related posts

1 comment

  1. 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:

    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;
        }
    //sample hook
    add_action('admin_init','sw_partner_id');
    

    After that try calling the function.
    I hope that helps.

Comments are closed.