WordPress 搜索文章自定义字段 search artilce postmeta

WordPress 插件开发中,有时需要检索文章对应的自定义字段postmeta,这里写一下相关的代码段

$searchArgs = array(
    'post_type' => 'post',
    'meta_query' = array(
        'relation' => 'AND',
        array( 'key' => 'srch_age',   'value' => $_REQUEST['srch_age'] ),
        array( 'key' => 'srch_ptype', 'value' => $_REQUEST['srch_ptype'] )
        // ... Add as many other criteria as you need
    )
);

query_posts( $searchArgs );
while( have_posts() ): the_post();
...
endwhile;

官方文档:https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注