Home > other >  Wordpress how can we get the featured image or other attached media from post
Wordpress how can we get the featured image or other attached media from post

Time:02-08

I am trying to get the featured image of my custom post type. I also have attachments in my custom post however I am not able to see the data. Am I using the wrong query method? In my json output I see general information however it is missing information such as feature image link.

add_filter("wcra_pictures_callback", "wcra_pictures_callback_handler");

function wcra_pictures_callback_handler($param) {
  //$param = All GET/POST values will be received from endpoint
  
    // WP QUERY
    $query = new WP_Query([
        'post_type' => $param['post_type'],
        'author' => $param['user_id'],
        'author_name' => $param['author'],
        'orderby' => $param['orderby'],
        'order'   => $param['order'],
        'posts_per_page' => $param['posts_per_page'],
    ]);

    // // Get Posts
    // $posts = get_posts([
    //     'post_type' => $param['post_type'],
    //     'author' => $param['user_id'],
    //     'author_name' => $param['author'],
    //     'orderby' => $param['orderby'],
    //     'order'   => $param['order'],
    //     'posts_per_page' => $param['posts_per_page'],
    // ]);
  
  return $query;
}

current out does not include data showing any attachments or links or feature image. How can I get that information.

{
   "status": "OK",
   "response": "Response OK",
   "code": 200,
   "data": {
       "query": {
           "post_type": "picture",
           "author": null,
           "author_name": null,
           "orderby": null,
           "order": null,
           "posts_per_page": null
       },
       "query_vars": {
           "post_type": "picture",
           "author": "",
           "author_name": "",
           "orderby": null,
           "order": "DESC",
           "posts_per_page": 10,
           "error": "",
           "m": "",
           "p": 0,
           "post_parent": "",
           "subpost": "",
           "subpost_id": "",
           "attachment": "",
           "attachment_id": 0,
           "name": "",
           "pagename": "",
           "page_id": 0,
           "second": "",
           "minute": "",
           "hour": "",
           "day": 0,
           "monthnum": 0,
           "year": 0,
           "w": 0,
           "category_name": "",
           "tag": "",
           "cat": "",
           "tag_id": "",
           "feed": "",
           "tb": "",
           "paged": 0,
           "meta_key": "",
           "meta_value": "",
           "preview": "",
           "s": "",
           "sentence": "",
           "title": "",
           "fields": "",
           "menu_order": "",
           "embed": "",
           "category__in": [],
           "category__not_in": [],
           "category__and": [],
           "post__in": [],
           "post__not_in": [],
           "post_name__in": [],
           "tag__in": [],
           "tag__not_in": [],
           "tag__and": [],
           "tag_slug__in": [],
           "tag_slug__and": [],
           "post_parent__in": [],
           "post_parent__not_in": [],
           "author__in": [],
           "author__not_in": [],
           "ignore_sticky_posts": false,
           "suppress_filters": false,
           "cache_results": true,
           "update_post_term_cache": true,
           "lazy_load_term_meta": true,
           "update_post_meta_cache": true,
           "nopaging": false,
           "comments_per_page": "50",
           "no_found_rows": false
       },
       "tax_query": {
           "queries": [],
           "relation": "AND",
           "queried_terms": [],
           "primary_table": "SQTSXGdhposts",
           "primary_id_column": "ID"
       },
       "meta_query": {
           "queries": [],
           "relation": null,
           "meta_table": null,
           "meta_id_column": null,
           "primary_table": null,
           "primary_id_column": null
       },
       "date_query": false,
       "request": "SELECT SQL_CALC_FOUND_ROWS DISTINCT SQTSXGdhposts.ID FROM SQTSXGdhposts  LEFT JOIN SQTSXGdhpostmeta AS myarmjoin ON (SQTSXGdhposts.ID = myarmjoin.post_id AND myarmjoin.meta_key = 'arm_access_plan' )  WHERE 1=1  AND ((SQTSXGdhposts.post_type = 'picture' AND (SQTSXGdhposts.post_status = 'publish' OR SQTSXGdhposts.post_status = 'acf-disabled'))) AND ( myarmjoin.post_id IS NULL )  ORDER BY SQTSXGdhposts.post_date DESC LIMIT 0, 10",
       "posts": [
           {
               "ID": 254,
               "post_author": "2",
               "post_date": "2022-02-07 16:53:39",
               "post_date_gmt": "2022-02-07 16:53:39",
               "post_content": "",
               "post_title": "Aurora_BG_7",
               "post_excerpt": "",
               "post_status": "publish",
               "comment_status": "open",
               "ping_status": "closed",
               "post_password": "",
               "post_name": "aurora_bg_7",
               "to_ping": "",
               "pinged": "",
               "post_modified": "2022-02-07 16:53:39",
               "post_modified_gmt": "2022-02-07 16:53:39",
               "post_content_filtered": "",
               "post_parent": 0,
               "guid": "https://cloud.elmc.app/picture/aurora_bg_7",
               "menu_order": 0,
               "post_type": "picture",
               "post_mime_type": "",
               "comment_count": "0",
               "filter": "raw"
           },
           {
               "ID": 249,
               "post_author": "2",
               "post_date": "2022-02-07 16:46:58",
               "post_date_gmt": "2022-02-07 16:46:58",
               "post_content": "",
               "post_title": "134909",
               "post_excerpt": "",
               "post_status": "publish",
               "comment_status": "open",
               "ping_status": "closed",
               "post_password": "",
               "post_name": "134909",
               "to_ping": "",
               "pinged": "",
               "post_modified": "2022-02-07 16:46:58",
               "post_modified_gmt": "2022-02-07 16:46:58",
               "post_content_filtered": "",
               "post_parent": 0,
               "guid": "https://cloud.elmc.app/picture/134909",
               "menu_order": 0,
               "post_type": "picture",
               "post_mime_type": "",
               "comment_count": "0",
               "filter": "raw"
           },
           {
               "ID": 147,
               "post_author": "1",
               "post_date": "2022-02-06 21:36:28",
               "post_date_gmt": "2022-02-06 21:36:28",
               "post_content": "",
               "post_title": "Scene 4 On",
               "post_excerpt": "",
               "post_status": "publish",
               "comment_status": "open",
               "ping_status": "closed",
               "post_password": "",
               "post_name": "scene-4-on",
               "to_ping": "",
               "pinged": "",
               "post_modified": "2022-02-06 21:36:28",
               "post_modified_gmt": "2022-02-06 21:36:28",
               "post_content_filtered": "",
               "post_parent": 0,
               "guid": "https://cloud.elmc.app/picture/scene-4-on",
               "menu_order": 0,
               "post_type": "picture",
               "post_mime_type": "",
               "comment_count": "0",
               "filter": "raw"
           },
           {
               "ID": 142,
               "post_author": "2",
               "post_date": "2022-02-06 03:49:57",
               "post_date_gmt": "2022-02-06 03:49:57",
               "post_content": "",
               "post_title": "No_video",
               "post_excerpt": "",
               "post_status": "publish",
               "comment_status": "open",
               "ping_status": "closed",
               "post_password": "",
               "post_name": "no_video",
               "to_ping": "",
               "pinged": "",
               "post_modified": "2022-02-06 03:49:57",
               "post_modified_gmt": "2022-02-06 03:49:57",
               "post_content_filtered": "",
               "post_parent": 0,
               "guid": "https://cloud.elmc.app/picture/no_video",
               "menu_order": 0,
               "post_type": "picture",
               "post_mime_type": "",
               "comment_count": "0",
               "filter": "raw"
           }
       ],
       "post_count": 4,
       "current_post": -1,
       "in_the_loop": false,
       "post": {
           "ID": 254,
           "post_author": "2",
           "post_date": "2022-02-07 16:53:39",
           "post_date_gmt": "2022-02-07 16:53:39",
           "post_content": "",
           "post_title": "Aurora_BG_7",
           "post_excerpt": "",
           "post_status": "publish",
           "comment_status": "open",
           "ping_status": "closed",
           "post_password": "",
           "post_name": "aurora_bg_7",
           "to_ping": "",
           "pinged": "",
           "post_modified": "2022-02-07 16:53:39",
           "post_modified_gmt": "2022-02-07 16:53:39",
           "post_content_filtered": "",
           "post_parent": 0,
           "guid": "https://cloud.elmc.app/picture/aurora_bg_7",
           "menu_order": 0,
           "post_type": "picture",
           "post_mime_type": "",
           "comment_count": "0",
           "filter": "raw"
       },
       "comment_count": 0,
       "current_comment": -1,
       "found_posts": 4,
       "max_num_pages": 1,
       "max_num_comment_pages": 0,
       "is_single": false,
       "is_preview": false,
       "is_page": false,
       "is_archive": true,
       "is_date": false,
       "is_year": false,
       "is_month": false,
       "is_day": false,
       "is_time": false,
       "is_author": false,
       "is_category": false,
       "is_tag": false,
       "is_tax": false,
       "is_search": false,
       "is_feed": false,
       "is_comment_feed": false,
       "is_trackback": false,
       "is_home": false,
       "is_privacy_policy": false,
       "is_404": false,
       "is_embed": false,
       "is_paged": false,
       "is_admin": false,
       "is_attachment": false,
       "is_singular": false,
       "is_robots": false,
       "is_favicon": false,
       "is_posts_page": false,
       "is_post_type_archive": true,
       "thumbnails_cached": false
   }
}

CodePudding user response:

You use the_post_thumbnail to output a featured image of a post while inside a while loop where you are changing the current post (by calling $query->the_post())

Outside of a 'WordPress' loop you can use get_the_post_thumbnail which accepts a post id as the first parameter.

Examples

Please note, in the two first examples the functions return the image within an tag, not the url of the image.

This would be suited to your situation as you have an instance of WP_Query to loop through.

$query = new WP_Query($args);

while($query->have_posts()){
    $query->the_post();
    the_post_thumbnail('full', ['class' => 'my image classes']); // 'full' is original uploaded size I believe.
    
    // Need image as a variable?
    // $image = get_the_post_thumbnail(get_the_ID(), 'full', ['class' => 'my image class'];
}
wp_reset_postdata(); // reset usage of 'the_post()'

Alternative: iterate using foreach on the array of posts.

$query = new WP_Query($args);

$posts = $query->posts; // now an array of posts
foreach($posts as $post){
    $image = get_the_post_thumbnail($post->ID, 'full', ['class' => 'my image classes'];
}

Obtaining image URL string only

// Inside 'WordPress' loop
$image = get_the_post_thumbnail_url(get_the_ID(), 'full');

// Inside foreach loop
$image = get_the_post_thumbnail_url($post->ID, 'full);

Documentation references

  •  Tags:  
  • Related