1

I want to add a meta data to my attachment posts, so I can get them in order by meta value later. That meta value would be 'price'.

So I thought this could work:

I have this args:

$args = array(
'order'          => 'ASC',
'post_type'      => 'attachment',
'post_parent'    => $post->ID,
'post_mime_type' => 'image',
'post_status'    => null,
'numberposts'    => -1,
);

Then I get posts (attachment posts) with $att_posts = get_posts($args);

Then I would like to add to each attachment post, a meta data called 'price', which value I get by get_post_meta($post->ID, 'key', true)['price'];.

So I thought this would do the work to add the meta data:

foreach($att_posts as $att){

wp_update_attachment_metadata( $att->ID, array("price"=>get_post_meta($post->ID, 'key',    
true)['price'])));

}

So i declared other args again but this time to order by 'meta_value_num', with meta key 'price'.

But it didn´t work.

Any body knows a simple way to make that possible? Any tips?

Thanks.

EDIT:

Here is all my code to get attachment images orderedby 'meta_key' 'price'.

$args = array(
'order'          => 'ASC',

'post_type'      => 'attachment',
'post_parent'    => $post->ID,
'post_mime_type' => 'image',
'post_status'    => null,
'numberposts'    => -1,
);


$att_posts = get_posts($args);

if ($att_posts) {
    foreach( $att_posts as $att ) {
        wp_update_attachment_metadata( $att->ID, array( 'price' => get_post_meta( 
$post->ID, 'price', true ) ) );
    }
}




$args = array(
'order'          => 'ASC',
'orderby'       =>'meta_value_num',
'meta_key'      =>'price',
'post_type'      => 'attachment',
'post_parent'    => $post->ID,
'post_mime_type' => 'image',
'post_status'    => null,
'numberposts'    => -1,
);



$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachment) {




    echo '<a href='.$surl.'/'.$post->post_name.'>'.wp_get_attachment_image($attachment-
>ID, 'thumbnail_large').'</a>';
    }
}else{ 
     echo '<a href='.$surl.'/'.$post->post_name.'>'.'<div class=\'thumbnail-small-img-   
search\'></div>'.'</a>';

}

But still doesn´t work.

Another way of asking is: How can I add meta data to attachment posts. And later, get the attachment posts ordered by this meta data called 'price'.

The objective of this is to display a paged list of posts with images ordered by price from cheapest to expensive.

This would be invoked by a submit button to one page of the website.

  • If this truly is all your code, it is really wrong. I have deleted my previous answer as a result. You need to better explain what you are trying to do. Do you want to retrieve the current post, then get all attachments for the post, then update the attachments' price metadata to be the same as that for the post, and then output all attachments ordered by price? And if so, how would the prices be different, given what you have? Clarification definitely needed; flagged. – user12479 Mar 17 '15 at 17:10
  • Hi. I want to add a meta data to my attachment posts, so I can get them in order by meta value later. That meta value would be 'price' – Marcelo Noronha Mar 17 '15 at 17:13
  • 1
    what is the problem with the question? It´s simple, I just want a way to get attachment posts ordered by a meta value 'price'. That I tried to add – Marcelo Noronha Mar 17 '15 at 17:16
  • What is the problem with the question? – Marcelo Noronha Mar 17 '15 at 18:35
  • Your edit rendered my answer completely incorrect. Your subsequent edits have made the question understandable, but you need to provide the context in which this code is being used. (Is it on a template? In a plugin? In functions.php? When is it invoked?) – user12479 Mar 17 '15 at 19:10
4

So since attachments are post types, you should be able to assign them postmeta like any other post type.

Using the update_post_meta function should get you where you need to be.

update_post_meta( $attachment_id, 'price', $price );

http://codex.wordpress.org/Function_Reference/update_post_meta

Then run your query based on a meta key.

$args = array(
'order'          => 'ASC',

'post_type'      => 'attachment',
'post_parent'    => $post->ID,
'post_mime_type' => 'image',
'post_status'    => null,
'numberposts'    => -1,

'meta_key'       => 'price',
);

http://codex.wordpress.org/Template_Tags/get_posts#Parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.