0

I'm adding custom meta to my attachment files, and want to store three page IDs per attachment in an array.

Then, I want to do a get_posts for attachments whos ID exist in this array, but the array is being returned as empty.

$p_downloads = get_posts(array(
    'post_type'      => 'attachment',
    'post_mime_type' => 'application/pdf',
    'meta_key'       => 'bv_media_meta_procedure',
    'meta_value'     => $post->ID,
    'orderby'        => 'title',
    'order'          => 'ASC',
    'posts_per_page' => -1
));

Currently the post meta of bv_media_meta_procedure for one of the attachments is looking like this:

array (size=1)
  0 => 
    array (size=3)
      0 => string '238' (length=3)
      1 => string 'null' (length=4)
      2 => string 'null' (length=4)

So on the page ID of 238, I expect to see the attachment being returned in the get_posts function.

But it's returning empty, how comes?

Lee
  • 4,187
  • 6
  • 25
  • 71

1 Answers1

0

I found out that after reading this article: http://brianshim.com/webtricks/query-wordpress-custom-field-array/

The custom meta is stored as serialised arrays, and by changing the get_posts function slightly, now returns the correct results.

$p_downloads = get_posts(array(
    'post_type'      => 'attachment',
    'post_mime_type' => 'application/pdf',
    'meta_key'       => 'bv_media_meta_procedure',
    'meta_value'     => '"'.$post->ID.'"',
    'meta_compare'   => 'LIKE',
    'orderby'        => 'title',
    'order'          => 'ASC',
    'posts_per_page' => -1
));
Lee
  • 4,187
  • 6
  • 25
  • 71