Wordpress:如何按高级自定义字段过滤帖子

时间:2015-10-17 19:29:53

标签: wordpress advanced-custom-fields

我试图在内部使用相同的ACF获取所有帖子。例如。理想情况下,我需要一个存档页面或自定义类别页面,其中包含包含给定字段的帖子。

尝试按照此处显示的步骤进行操作:http://www.advancedcustomfields.com/resources/acf-fields-post_object-query/

并在functions.php中创建过滤器,如下所述:

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);

但是不太明白如何在某个给定页面上发帖...例如localhost/?<customfieldname>=<customfield_value>

1 个答案:

答案 0 :(得分:0)

您最好的办法是为其创建页面或存档模板,并将其与以下查询结合使用:

<?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
));
?>

ACF文档中的更多信息: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/

有关WP中模板的更多信息: https://codex.wordpress.org/Creating_an_Archive_Index &安培;在这里:https://developer.wordpress.org/themes/basics/template-hierarchy/#visual-overview