0
votes

I am trying to get all posts with the same ACF inside. E.g. ideally I need an archive page, or a custom category page, with posts which contain given field.

Was trying to follow steps shown here: http://www.advancedcustomfields.com/resources/acf-fields-post_object-query/

And created filters in functions.php as described there:

function my_post_object_query( $args, $field, $post )
{
    // modify the order
    $args['orderby'] = 'title';

    return $args;
}

// filter for every field
add_filter('acf/fields/post_object/query', 'my_post_object_query', 10, 3);

// filter for a specific field based on it's name
add_filter('acf/fields/post_object/query/name=my_select', 'my_post_object_query', 10, 3);

// filter for a specific field based on it's key
add_filter('acf/fields/post_object/query/key=field_508a263b40457', 'my_post_object_query', 10, 3);

But don't quite understand how to get posts on some given page... e.g. localhost/?<customfieldname>=<customfield_value>

1

1 Answers

0
votes

Your best bet is to create a page or archive template for that, and combine it with the following query:

<?php 
$posts = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'post',   // could also be page or your custom post_type
    'meta_key'      => 'color',  // your ACF field name
    'meta_value'    => 'red'     // Your ACF field value
));
?>

More info in ACF docs here: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/

More info on templates in WP here: https://codex.wordpress.org/Creating_an_Archive_Index & here: https://developer.wordpress.org/themes/basics/template-hierarchy/#visual-overview