admin管理员组

文章数量:1122846

I have a ACF select field which takes multiple value. Now I want to use get_posts() to get those custom posts. My arguments look like this:

$party = 'test1';
$function = 'test1, test2';
$args = array(
    'numberposts'   => -1,
    'post_type'     => 'event',
    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'key'       => 'party',
            'compare'   => '=',
            'value'     => $party,
        ),
        array(
            'key'       => 'function',
            'compare'   => 'IN',
            'value'     => array($function),
        )
    )
);
$items = get_posts($args);

But this does not work! Don't know what is wrong here!

I have a ACF select field which takes multiple value. Now I want to use get_posts() to get those custom posts. My arguments look like this:

$party = 'test1';
$function = 'test1, test2';
$args = array(
    'numberposts'   => -1,
    'post_type'     => 'event',
    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'key'       => 'party',
            'compare'   => '=',
            'value'     => $party,
        ),
        array(
            'key'       => 'function',
            'compare'   => 'IN',
            'value'     => array($function),
        )
    )
);
$items = get_posts($args);

But this does not work! Don't know what is wrong here!

Share Improve this question asked May 26, 2016 at 7:27 Imrul.HImrul.H 1752 silver badges14 bronze badges 2
  • Why you don't try with: $items = new WP_Query( $args ); WP_Query Reference. – emilushi Commented May 26, 2016 at 11:16
  • What are the types of these ACF fields? – Krzysiek Dróżdż Commented Nov 29, 2018 at 14:27
Add a comment  | 

1 Answer 1

Reset to default 0

This is not the way which you have clled for ACF.

Parameters

<?php $field = get_field($field_name, $post_id, $format_value); ?>
  • $field_name: the name of the field to be retrieved. eg “page_content” (required)
  • $post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc
  • $format_value: whether or not to format the value loaded from the db. Defaults to true (not required).

Usage

Get a value from the current post

$value = get_field( "text_field" );

Get a value from a specific post

$value = get_field( "text_field", 123 );

Check if value exists

$value = get_field( "text_field" );

if( $value ) {

    echo $value;

} else {

    echo 'empty';

}

Get a value from other places

$post_id = null; // current post
$post_id = 1;
$post_id = "option";
$post_id = "options"; // same as above
$post_id = "category_2"; // target a specific category
$post_id = "event_3"; // target a specific taxonomy (this tax is called "event")
$post_id = "user_1"; // target a specific user (user id = 1)

$value = get_field( "text_field", $post_id );

Get a value without formatting

In this example, the field image is an image field which would normally return an Image object. However, by passing false as a 3rd parameter to the get_field function, the value is never formatted and returned as is from the Database.

Please note the second parameter is set to false to target the current post

$image = get_field('image', false, false);

本文标签: advanced custom fieldsGet posts with multiple meta values