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
  • 2 Code seems ok, how do you get $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
  • 2 You are going to need to show where you get $user from - it is presumed this is an object, probably a WP_User object, but it is not clear in your code - this could also be empty and giving a PHP error. – Q Studio Commented Jan 20, 2021 at 11:10
  • 1 Note that you can't pull things out of thin air, if you used $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
Add a comment  | 

1 Answer 1

Reset to default 0

As 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