July 24, 2018 at 3:46 pm #9143
Hi, Is there anyway to to add attachments using piklist rather than adding them in the content area via the “insert Media” button in the wysiwyg?
I don’t want to use a custom field, I want to select them via a piklist field and then store them how wordpress stores them – as attachments.
I then want to use something like “get_attached_media( ‘image’, $post_id )” to get the attachments.
I did try to use the code from the demo: https://gist.github.com/beatelite/eecbbd5b301ac62583b9245af5547278
However, it’s doing this weird thing where when I select three images and then publish, all three images become the first one selected. https://drive.google.com/open?id=1MfNye3lvLrzwnu59TQ62d4AykZahzm2y
What am I missing?
July 24, 2018 at 4:41 pm #9144
Btw, Running 0.11.4
July 25, 2018 at 12:28 pm #9156
The featured image demo is the way to go. Not sure why all three images are the same. I just tested and it works fine.
Does the featured image box work in the demos?
July 25, 2018 at 1:06 pm #9160
Yes, it does. I tried it on another post type and I’m still having the same issue.
I did notice that the demo does away with the traditional featured media box. Would that need to be done in order for it to work?
I dug around the demo code to see how you removed it but I didn’t see anything specific. Let me know if you have any other ideas. As always, thanks for your help, Steve!
July 25, 2018 at 2:19 pm #9167
July 25, 2018 at 2:29 pm #9169
Update: This works once I hid the featured media box: https://piklist.com/support/topic/hide-the-feature-image-default-box/
,'hide_meta_box' => array( /* here the Feature image div */ ,'postimagediv' )
The images now appear correctly. However, now I have a new issue. I can’t get them via any wp method.
global $post; $attachments = get_posts( array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'any', 'post_parent' => $post->ID, 'exclude' => get_post_thumbnail_id(), ) );
$attachments = get_children(array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID'));
The only thing I can get is the the post thumbnail with:
get_post_thumbnail_id( $post->ID );
It looks like piklist isn’t actually attaching these extra images to the custom post type or I’m missing something. Any advice is appreciated.
July 25, 2018 at 2:39 pm #9170
One more update: I noticed that the following code:
get_post_meta( $post->ID, '_thumbnail_id', false );
gets the ids of all the attachments. That’s great and all but I am trying to avoid custom post meta to pull them. I was anticipating using native wp functions like (get_posts etc) to get the attachments because I have a frontend page builder that uses those functions to grab attachments and I don’t really want to hack into it.
Let me know if this is possible.
July 25, 2018 at 2:52 pm #9171
There is no place in the default WordPress
posttable to save attachment data.
July 25, 2018 at 3:40 pm #9172
Ok, So there’s no way to use piklist to save attachments and use native wp methods to get those attachements?
Also, why does the piklist demo for uploading featured images say this:
Use this field to match WordPress featured image field name.
July 25, 2018 at 3:43 pm #9173
Everything about Piklist is done the WordPress way including retrieving data. You can use the native WordPress function get_post_meta to retrieve the attachment information.
As for matching the WP featured image field name… if you are replacing the default WordPress field, then you need to use the same field name.
July 25, 2018 at 3:54 pm #9174
“Everything about Piklist is done the WordPress way including retrieving data. You can use the native WordPress function get_post_meta to retrieve the attachment information.”
I get that (and really appreciate it!) However, I think i should rephrase that:
Can piklist add media to a post by updating the attachment’s parent field to be the id of the post we’re attaching it to?
For example, this is how I would do it if I was writing something custom:
wp_update_post( array( 'ID' => $attachment_id, 'post_parent' => $post_id ) );
July 25, 2018 at 4:21 pm #9175
To get $attachment_id you would need to write some JS to detect the id of the image.
Or I guess you can also save the image to the post_meta table and get the id there. You could run a filter on
save_postas 9999, and pull the meta after it’s saved. Totally untested… 😉
- You must be logged in to reply to this topic.