Home > Blockchain >  How to retrieve at Frontend the values of WordPress custom fields created for Media Library Images (
How to retrieve at Frontend the values of WordPress custom fields created for Media Library Images (

Time:06-09

I have 2 different working methods at functions.php for backend. Each method below has 2 hooks; 1 to display the new custom field & another hook to save\update the values:

Method 1:

function media_hacks_attachment_field_to_edit( $form_fields, $post ){
    // https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata
    $media_author = get_post_meta( $post->ID, 'media_author', true );
    
    $form_fields['media_author'] = array(
        'value' => $media_author ? $media_author : '',
        'label' => __( 'Author' )
    ); 
    return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'media_hacks_attachment_field_to_edit', null, 2 );

//Saving value on Update (method 1)
function media_hacks_edit_attachment( $attachment_id ){
    if ( isset( $_REQUEST['attachments'][$attachment_id]['media_author'] ) ) {
    
        $media_author = $_REQUEST['attachments'][$attachment_id]['media_author'];
    
        update_post_meta( $attachment_id, 'media_author', $media_author );
    }
}
add_action( 'edit_attachment', 'media_hacks_edit_attachment' );

Method 2:

function my_image_attachment_fields_to_edit($form_fields, $post) {
    // $form_fields is a special array of fields to include in the attachment form
    // $post is the attachment record in the database
    //     $post->post_type == 'attachment'
    // (attachments are treated as posts in Wordpress)
     
    // add our custom field to the $form_fields array
    // input type="text" name/id="attachments[$attachment->ID][custom1]"
    $form_fields["custom1"] = array(
        "label" => __("Custom Text Field"),
        "input" => "text", // this is default if "input" is omitted
        "value" => get_post_meta($post->ID, "_custom1", true)
    );
    // if you will be adding error messages for your field, 
    // then in order to not overwrite them, as they are pre-attached 
    // to this array, you would need to set the field up like this:
    $form_fields["custom1"]["label"] = __("Custom Text Field");
    $form_fields["custom1"]["input"] = "text";
    $form_fields["custom1"]["value"] = get_post_meta($post->ID, "_custom1", true);
     
    return $form_fields;
}
// attach our function to the correct hook
add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", null, 2);

//Saving value on Update (method 2)
function my_image_attachment_fields_to_save($post, $attachment) {
    // $attachment part of the form $_POST ($_POST[attachments][postID])
    // $post attachments wp post array - will be saved after returned
    //     $post['post_type'] == 'attachment'
    if( isset($attachment['custom1']) ){
        // update_post_meta(postID, meta_key, meta_value);
        update_post_meta($post['ID'], '_custom1', $attachment['custom1']);
    }
    return $post;
}
add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null, 2);

Here's the good result at backend Media Library (Custom Text Field & Author):

enter image description here

This was it for the Backend dashboard.


My question is for the Frontend:

Now how can I retrieve & display values of these 2 custom fields at the FRONTEND?

Here's my failed try at a template php page:

<tr id='MySpecialRow'>
    <td colspan='2' style='background:#000;color:#fff;'>
      <?php

        $args = array('cat' => 8);

        $query = new WP_Query($args);
        if ($query->have_posts()) {
          // some code here if you want.
          while ($query->have_posts()) {
            $query->the_post();

            $untitled_meta = rwmb_meta('image_advanced_8hswqfsoqai', '', get_the_ID());
            foreach ($untitled_meta as $image) {

              $media_author = get_post_meta( get_the_ID(), 'media_author', true );
              echo get_the_ID();//correctly prints post id
              echo $media_author;//prints nothing :(

            }
          }
        }
      ?>
    </td>
</tr>

Small notes:

  • get_the_ID() does print the post id, but $media_author has no value :(

  • I'm doing a WordPress posts query loop because the gallery containing the custom fields exists in a Post. In other words I don't have the post Id since I'm at a Page template.

CodePudding user response:

The array you got has the image post object ID as the array keys, so you need to use the extended foreach syntax to get access to the key as well.

foreach ($untitled_meta as $id => $image) {
  $media_author = get_post_meta( $id, 'media_author', true );

Normally when looping over array data you rather seldom need access to the key as well, but when you do, PHP offers the $key => $value syntax to get access to the key as well, https://www.php.net/manual/en/control-structures.foreach.php

  • Related