filter get_posts with ACF fields, nested

1k views Asked by At

I have several post types:

  • Products
  • Reviews
  • Showcases
  • Manufacturers

Those are the two most important for this question

Reviews and Showcases are about a product, so when an user adds a review or showcase, they have to select the product trough an ACF relationship field.

When a user adds a product, they have to select the Manufacturer trough an ACF post object field

I have created all my custom pages for the reviews, product and showcases, now I arrived at the Manufacturer post type. What I want here and am unable to achieve is show the latest 5 reviews, products and showcases with this manufacturer.

I know how to create a query etc, but have no idea what arguments to set in order to filter reviews and showcases (they work the same way, two levels nested) and products (one level nested) for that specific manufacturer.

Can somebody please post me in the right direction?

1

There are 1 answers

0
radscheit On BEST ANSWER

When you create an Query, you can ask for different post types in a query with givin an array of post types:

$args = array(
'post_type' => array( 'post', 'page', 'movie', 'book' )
);
$query = new WP_Query( $args );

In addition to that you can make a meta_query within this WP_Query in order to ask for the associated manufacturer:

$posts = get_posts(array(
'numberposts'   => -1,
'post_type'     => 'post',
'meta_query'    => array(
    'relation'      => 'AND',
    array(
        'key'       => 'color',
        'value'     => array('red', 'orange'),
        'compare'   => 'IN',
    ),
    array(
        'key'       => 'featured',
        'value'     => '1',
        'compare'   => '=',
    ),
),
));

See the docs here.