Viewing 11 reply threads
  • Author
    Posts
    • #9143
      friendlyfire3
      Participant

      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?

    • #9144
      friendlyfire3
      Participant

      Btw, Running 0.11.4

    • #9156
      Steve
      Keymaster

      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?

    • #9160
      friendlyfire3
      Participant

      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!

    • #9167
      Steve
      Keymaster

      Yup… You need to remove the default featured image metabox or else you are duplicating the field.

      This doc explains how to remove it: https://docs.piklist.com/faq/fields-not-saving/

      Just replace custom-fields with thumbnail and post with your CPT.

    • #9169
      friendlyfire3
      Participant

      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.

      I’ve tried:

      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(),
      ) );

      and

      $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.

    • #9170
      friendlyfire3
      Participant

      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.

    • #9171
      Steve
      Keymaster

      There is no place in the default WordPress post table to save attachment data.

    • #9172
      friendlyfire3
      Participant

      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.

    • #9173
      Steve
      Keymaster

      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.

    • #9174
      friendlyfire3
      Participant

      “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
          )
      );
    • #9175
      Steve
      Keymaster

      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_post as 9999, and pull the meta after it’s saved. Totally untested… 😉

Viewing 11 reply threads
  • You must be logged in to reply to this topic.