sql - Get images by custom field -
i'm trying display images have custom field the types plugin set true
. work filter them post_content
or post_excerpt
none of attempts have worked far.
<? $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_content' => 'foo', 'numberposts' => -1 ); ?> <? print_r(get_posts($args)); ?>
this get's all images allthough one has post_content foo
. effort utilize wp_query
failed miserably well.
any help appreciated!
wp_query method :
$args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'meta_query' => array( array( 'key' => 'color', 'value' => 'blue', 'compare' => 'like', ), ), ); $query = new wp_query( $args );
i presuming reason why failed wp_query due next condition.
codex states : the default wp_query sets 'post_status'=>'publish', attachments default 'post_status'=>'inherit' you'll need explicitly set post_status 'inherit' or 'any' well.
http://codex.wordpress.org/class_reference/wp_query#custom_field_parameters
get_posts
method :
$args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'meta_key' => 'custom-field', 'meta_value' => 'custom value', 'numberposts' => -1 ); print_r(get_posts($args));
the draw method meta_value
needs match entered in custom field. if still utilize get_posts
utilize meta_query
shown in wp_query illustration above.
sql wordpress
No comments:
Post a Comment