0

I've added some custom fields to a rest route. I want to retrive the post thumbnail for my CPT and I'm using this code:

This is the code I'm using to add the post meta to the CPT after an image is selected and the post is published. If not clear I'm not using the default WP UI but a customnized one based on vue.js

                    $post_published = wp_update_post( 
                        array(
                            'ID' => $post_id,
                            'meta_input' => array(
                                'post_thumbnail' => get_the_post_thumbnail_url( $post_id )
                            ),
                            'post_status' => 'publish'
                        ), 
                        true, 
                        true 
                    );

The url will look like this localhost/wp-content/uploads/23/09/myimage.png and into the wp-admin custom dashboard the system will try to load this kind of url http://localhost/mysite/wp-admin/localhost/mysite/resources/uploads/2023/09/customimage-min-2.png

Anyway I've noticed that in my customized wordpress dashboard the link will be loaded in a wrong way, by inspecting the DOM I've noticed that the url of the thumbnail will be appended to the wp-admin one and this will result in a failed load. Is there a way to fix this?

7
  • Just to be clear, this post_thumbnail is not the featured image that comes with WordPress but a brand new and totally separate post meta? Featured images aren't stored as URLs, and it's not ideal to store raw URLs in the database if they're referring to posts as it deprives you the opportunity to filter them and can leave you with stale values when things change and the need to perform a search replace. E.g. if the thumbnail attachment was deleted there'd be no way to tell from that meta value if it was still valid without a slow DB query or a HTTP request, which isn't an issue with IDs
    – Tom J Nowell
    Oct 3, 2023 at 11:49
  • post thumbnail is a custom value I'm storing into the database, in this case I'm saving the url of the uploaded file. I need to have a cleean url to be able to load it using REST API since at the moment also the editor isn't the default one of WP
    – ICTDEV
    Oct 3, 2023 at 12:25
  • hmm that's useful to know but answered none of my questions. Can you expand further upon where/how/when the PHP code you mentioned in your question is executed? Also the sentence that comes after your code block is very difficult to parse and understand, can you rewrite it, as well as what you meant by "loaded in a wrong way"? Try to avoid implied statements and state things directly and unambiguously. Also include the full raw version of what got saved and stored in the database, it looks like the examples in your question are the final output
    – Tom J Nowell
    Oct 3, 2023 at 13:24
  • @TomJNowell the code is executed after post is inserted into the database using insert post function. Since I want to be able to see all the files inside the standard wordpress UI I've used the wp_upload_media function to upload the image. At the moment I'm thinking to switch to the default Wordpress UI for CPT, but in this case I'm unable to see the featureb image box also if I enable the feature when the cpt is registered, and also by adding theme support
    – ICTDEV
    Oct 5, 2023 at 9:24
  • is that in a REST API endpoint/Admin AJAX handler/a hook on the core REST API endpoints? How is $post_id being set? There's not enough code to diagnose the problem with what you shared. It's also unclear how you're testing this value wether it's the database value or if it's in the raw JSON response from the API, or if it's just the final processed output in the DOM once all code has executed
    – Tom J Nowell
    Oct 5, 2023 at 12:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.