查询自定义 post 类型,在查询中使用自定义 field/meta 键

Query custom post type, using custom field/meta key in the query

我使用工具集类型设置了多个 Wordpress 自定义 post 类型,名称如下:

无 9

无 8

没有-7

等等

我想进行 Wordpress 查询以在另一页上显示这些自定义 post 类型之一的 post。我想显示 post 的页面还包含一个名称为 'issue-no' 的自定义字段,该字段与我想显示的自定义 post 类型的名称匹配。

到目前为止,我的查询是:

        <?php 
            query_posts(array( 
                'post_type' => 'no-9',
            ) );  
        ?>
        
        <?php while (have_posts()) : the_post(); ?>
                <h2><?php the_title(); ?</h2>
        <?php endwhile;?>

这可以显示 post 类型 'no-9' 中的所有 post,但是我希望调用是动态的,以便它可以根据匹配的自定义字段进行更新 'issue-no'.

如何在查询中调用自定义字段 name/meta 键?理论上类似于下面的内容,但是它不会将自定义字段放入查询中。

        <?php 
            query_posts(array( 
                'post_type' => 'wpcf-issue-no',
            ) );  
        ?>
        
        <?php while (have_posts()) : the_post(); ?>
                <h2><?php the_title(); ?</h2>
        <?php endwhile;?>

最好制作一个像 $cpt 这样的变量并将您的自定义字段分配给它。不确定自定义字段使用什么插件,但我使用 ACF。

$cpt = get_field('Your_post_type_name');

在此之后将 $cpt 放在 query_post 上,如下所示:

query_posts(array( 
    'post_type' => $cpt,
) );

它应该可以工作。

所以它应该看起来像这样:

    <?php 
    $cpt = get_field('your_selector');
        query_posts(array( 
            'post_type' => $cpt,
        ) );  
    ?>
    
    <?php while (have_posts()) : the_post(); ?>
            <h2><?php the_title(); ?</h2>
    <?php endwhile;?>