具有ACF的Wordpress查询附件字段

时间:2018-09-13 09:14:53

标签: wordpress advanced-custom-fields shortcode

我正在尝试返回媒体库中具有自定义字段且值为True的所有图像。

由于某种原因,什么也没有退回,这是我到目前为止所拥有的:

function showPainted() {
    $query_images_args = array(
        'post_type'      => 'attachment',
        'post_mime_type' => 'image',
        'post_status'    => 'inherit',
        'posts_per_page' => - 1,
        'meta_key'      => 'show_on_painted_page',
        'meta_value'    => '1'
    );

    $query_images = new WP_Query( $query_images_args );
    $images = array();
    foreach ( $query_images->posts as $image ) {
        $images[] = wp_get_attachment_url( $image->ID );
    }

    print_r($images);
}
add_shortcode( 'showPainted', 'showPainted' );

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$query_images_args = array(
    'post_type'         => 'attachment',
    'post_mime_type'    => 'image',
    'post_status'       => 'inherit',
    'posts_per_page'    => - 1,
    'meta_key'          => 'show_on_painted_page',
    'meta_query'        => array(
        array(
            'key'       => 'show_on_painted_page',
            'value'     => 1,
            'compare'   => '='
        ),
    ),
);

检查https://codex.wordpress.org/Class_Reference/WP_Query了解更多信息。