You are not too far away from your goal. The get_calendar_ids
must return only $calendars
.
In your function $wpdb->get_results( $sql )
will return an object with the datas your asked.
function get_calendar_ids() {
global $wpdb;
$user_id = get_current_user_id();
$sql = $wpdb->prepare( "
Select
wp_calendars.user_id,
wp_calendars.name,
wp_calendars.id
From
wp_calendars
Where
wp_calendars.user_id = '$user_id'
");
$calendars = $wpdb->get_results( $sql );
if ($calendars ) {
return $calendars;
}
}
Your function now return an object with only user_id, name, and id, the result must in a loop to echo the result, you can grab them like this $calendar->name in a foreach loop.
an example in a shortcode,
add_shortcode('calendars', 'get_calendars');
function get_calendar($atts, $content = ""){
// you can add $atts extract_args if need
$calendars = get_calendar_ids();
if($calendars){
foreach($calendars as $calendar){
$user_data = get_userdata(get_current_user_id());
echo '<a href="'.get_permalink($calendar->id).'">'.$calendar->name . '</a> by '.$user_data->user_nicename.'</br>';
}
}
}
Hope it helps