[wp-hackers] A Basic Activity Feed - Is This Efficient?
J.D. Grimes
jdg at codesymphony.co
Tue Oct 29 12:54:11 UTC 2013
Any activity feed will require a lot of database queries, and yours probably requires less than BuddyPress’s. I would suggest that you put the limit directly in the SQL query. Right now you are pulling more entries from the DB than you need. So:
$activity_calls = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed WHERE user_id = %d ORDER BY id ASC LIMIT %d', $author_id, $limit ) );
Abdussamad’s solution for the activity message is good. Just FYI, you could also have used a switch statement:
switch ( $row->activity_type ) {
case ‘new_record’:
$message = //…
$points = //…
break;
case ‘deleted_record’:
//...
}
But his solution is better.
-J.D.
On Oct 29, 2013, at 8:34 AM, J.D. Grimes <jdg at codesymphony.co> wrote:
> This right here is open to sql injection, as Abdussamad pointed out:
>
>> $activity_calls = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix .
>> 'jo_activity_feed' .' WHERE user_id = '. $author_id .' ORDER BY id ASC');
>
> You should be using $wpdb->prepare():
>
> $activity_calls = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix .
> 'jo_activity_feed WHERE user_id = %d ORDER BY id ASC', $author_id ) );
>
> -J.D.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
More information about the wp-hackers
mailing list