Reputation: 79
Trying to create a short code that will only allow a logged in user to see a post if they are the assigned 'user' in the custom field (ACF) of that post. The below code brings back "No results were found" despite the criteria being met. Can anyone see what is wrong?
add_shortcode( 'show_profile_posts', 'profile_posts' );
function profile_posts( $atts ) {
global $post;
$output = '';
$args = array(
'numberposts' => -1,
'post_type' => 'profile',
'meta_key' => 'user_id',
'meta_value' => $current_user->ID
);
$fe_query= new WP_Query( $args );
if ( $fe_query->have_posts() ) {
$output .= '<ul class="fe-query-results-shortcode-output">';
while ( $fe_query->have_posts() ) {
$fe_query->the_post();
$title = get_the_title();
$link = get_the_permalink();
$output .= "<li><a href=\"{$link}\">{$title}</a></li>";
}
$output .= '</ul>';
} else {
$output .= '<div class="fe-query-results-shortcode-output-none">No results were found</div>';
}
wp_reset_postdata();
return $output;
}
Upvotes: 0
Views: 55
Reputation: 21
add_shortcode( 'show_profile_posts', 'profile_posts' );
function profile_posts( $atts ) {
global $post;
$user_id = get_current_user_id();
$output = '';
$args = array(
'numberposts' => -1,
'post_type' => 'profile',
'meta_query' => array(
array(
'key' => 'user_id',
'value' => $user_id,
'compare' => '=',
),
),
);
$fe_query= new WP_Query( $args );
if ( $fe_query->have_posts() ) {
$output .= '<ul class="fe-query-results-shortcode-output">';
while ( $fe_query->have_posts() ) {
$fe_query->the_post();
$title = get_the_title();
$link = get_the_permalink();
$output .= "<li><a href=\"{$link}\">{$title}</a></li>";
}
$output .= '</ul>';
} else {
$output .= '<div class="fe-query-results-shortcode-output-none">No results were found</div>';
}
wp_reset_postdata();
return $output;
}
Upvotes: 0