Skip to content

Retrieve Attachments Outside The Loop doesn't work #189

Description

@im4aLL

For custom post type - retrieving attachments don't work

$query = new WP_Query([
	'post_type'        => 'news',
	'posts_per_page'   => 5,
]);

echo '<pre>';
print_r($query->posts);
echo '</pre>';

wp_reset_query();

$attachments = new Attachments( 'attachments', 13 ); // post ID 13 has 4 attachments
echo $attachments->exist() ? 'yes' : 'no'; // result is - no. Thought it has data!

Temporary fix

  • Use get_post_meta to retrive post meta where meta key is attachments example - get_post_meta( $post_id, 'attachments', true ) then json decode
  • You will receive an array by your registered attachment eg. my_attachments
  • Then loop through result and use wp_get_attachment_url to get URL.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions