admin管理员组文章数量:1310466
I am having issues with my WordPress query. I am not the best coder at all, and I usually just get by learning here and there and editing/writing code as needed. I worked on a few things pretty much all night on Monday and I seem to be having an issue that I could use a fresh set of eyes on. I am pretty sure the issue is with the meta_value, but then again I am just not experienced enough to get past this point. Any help/suggestions would be greatly appreciated.
The below code correctly pulls the shipment posts, but it is pulling all posts no matter the meta_value I am searching for.
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'shipments',
'meta_key' => 'customer_user_id',
'meta_value' => $user->ID
));
if($posts)
{
echo '<ul>';
foreach($posts as $post)
{
echo "My code here";
}
}
I am having issues with my WordPress query. I am not the best coder at all, and I usually just get by learning here and there and editing/writing code as needed. I worked on a few things pretty much all night on Monday and I seem to be having an issue that I could use a fresh set of eyes on. I am pretty sure the issue is with the meta_value, but then again I am just not experienced enough to get past this point. Any help/suggestions would be greatly appreciated.
The below code correctly pulls the shipment posts, but it is pulling all posts no matter the meta_value I am searching for.
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'shipments',
'meta_key' => 'customer_user_id',
'meta_value' => $user->ID
));
if($posts)
{
echo '<ul>';
foreach($posts as $post)
{
echo "My code here";
}
}
Share
Improve this question
edited Jan 20, 2021 at 10:56
Andrew Giardina
asked Jan 20, 2021 at 10:50
Andrew GiardinaAndrew Giardina
112 bronze badges
3
|
1 Answer
Reset to default 0As I said, your code seems ok, but I think you dont get the current user beforehand.
Something like this works:
$user = wp_get_current_user(); // get current user, to get the ID
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'shipments',
'meta_key' => 'customer_user_id',
'meta_value' => $user->ID
));
if( $posts ){
echo '<ul class="shipment-posts-list">';
foreach( $posts as $post ){
// get post data
$post_id = $post->ID; //current post ID
$post_title = $post->post_title; // current post title
$post_thumbnail = get_the_post_thumbnail( $post_id ); // current post thumbnail
// get post metadata
$shipment_tracking = get_post_meta($post_id, 'tracking_number', true);
$customer_user_id = get_post_meta($post_id, 'customer_user_id', true);
echo '<li class="shipment-'.$post_id.'">';
// example:
echo '<h3>'.$post_title.'</h4>'; // show post title
echo $post_thumbnail; // show post thumbnail
echo $customer_user_id; // show customer user id
echo '</li>';
}
echo '</ul>';
}
Also before you edited your question, you tried to get meta data in the wrong way.
Get meta data like:
$shipment_tracking = get_post_meta($post_id, 'tracking_number', true);
Notice the $post_id
part. (instead of just $post
like you had).
本文标签: plugin developmentIssue with querying
版权声明:本文标题:plugin development - Issue with querying 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741819279a2399266.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$user
to get the$user->ID
? You could get the current user with$user = wp_get_current_user();
, after this you can use$user->ID
. – LWS-Mo Commented Jan 20, 2021 at 11:09$user->ID
without acquiring or fetching the$user
variable, you'll be seeing a PHP notice/warning in the error log and it will be substituting""
or the value – Tom J Nowell ♦ Commented Jan 20, 2021 at 11:24