按自定义字段查询帖子

Query posts by custom fields

我正在使用 Advanced Custom Fields 建立一个出售游艇的网站。游艇使用自定义 Post 类型,称为“游艇”

单个游艇列表中的一个字段是一个 true/false 值,用于确定它是否应该是特色列表。 (它最初是一个复选框,但我在尝试这个 post 中的答案后更改了它)

我正在尝试在主页上显示精选列表的预览,但无法正常工作。

我最初尝试使用 ACF 文档中的这段代码

编辑: 我开始只是尝试获取标题,但我还需要其他字段

<section class="featured-yachts">
    
    <h2>FEATURED YACHTS</h2>
    
    <?php 
    
    // args
    $args = array(
        'numberposts'   => -1,
        'post_type'     => 'yachts',
        'meta_key'      => 'featured',
        'meta_value'    => 'yes'
    );
    
    
    // query
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if( $the_query->have_posts() ): ?>
        <ul>
        <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                </a>
            </li>
        <?php endwhile; ?>
        </ul>
    <?php endif; ?>
    
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
    
    
    
</section>  

现在切换到真假字段后就有了这个

<section class="featured-yachts">
    
    <h2>FEATURED YACHTS</h2>
    
    <?php 
    
    $args = array(
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'featured',
                'value' => '1',
                'compare' => 'LIKE',
            )
        ),
    );

    $my_posts = new WP_Query($args);

    if ($my_posts->have_posts()) {

        while ($my_posts->have_posts()) : $my_posts->the_post();

          echo get_the_title();

        endwhile;
    wp_reset_postdata();

}
    
    ?>
    
</section>  

我也试过编辑这个:

但是,我还是无法让它工作。

其中最多的 return 是 h3 标题

我不知道是什么问题。

在此先感谢您的帮助

edit : 我至少有一个 post 使用自定义 post 类型 'yachts' 设置为 true for featured.

我还有很多字段要添加,但我希望这会输出 h3 标题,然后输出 post 标记为特色的标题。

最后,我希望它的功能与最新的 post 预览非常相似,但显示自定义字段并且仅当 'featured' true/false 设置为 yes

我创建了一个名为 'featured yachts' 的 ACF 字段并向 Gutenberg 注册了该块,但其中没有实际的 ACF 字段,它仅用于调用具有此代码的文件 'content-featured-yachts-block.php'我正在尝试修复。

我正在尝试使用从各个游艇列表中提取的数据填充此特色游艇块中的 post 预览。在这些列表中有 'featured' true/false 选项。附上屏幕截图。

这是我注册CPT的代码

// Our custom post type function
function create_posttype() {
 
    register_post_type( 'yachts',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Yacht Listings' ),
                'singular_name' => __( 'Yacht' ),
                'supports' => array( 'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail' ),
                'add_new' => __( 'New Yacht Listing'),
                 'add_new_item' => __( 'Add New Yacht Listing'),
                 'edit_item' => __( 'Edit Yacht Listing'),
                 'new_item' => __( 'New Yacht Listing'),
                 'view_item' => __( 'View Listings'),
                 'search_items' => __( 'Search Listings'),
                 'not_found' =>  __( 'No Yacht Listings Found'),
                 'not_found_in_trash' => __( 'No yacht Listings found in Trash')
            ),
            'public' => true,
            // 'has_archive' => true,
            'rewrite' => array('slug' => 'yachts'),
            'show_in_rest' => true,
            'menu_icon' => 'dashicons-sos',
            'hierarchical' => true,
            'taxonomies' => array('category')
        )
    );
}

另一个编辑 - 我假设如果我能显示标题字段,其他字段会很容易,但我也不能得到它们所以我也需要弄清楚如何从列表中添加其他字段

我试过的代码如下

<h2>FEATURED YACHTS</h2>

<?php 

$args = array(
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'featured',
            'value' => 'yes',
            'compare' => 'LIKE',
        )
    ),
);

$my_posts = new WP_Query($args);

if ($my_posts->have_posts()) {

    while ($my_posts->have_posts()) : $my_posts->the_post();?>

    <h2><?php the_title(); ?></h2>
    
    <?php
    get_post_meta( get_the_ID(), 'price', true );
    ?>

    <?php endwhile;
wp_reset_postdata();

}
?>

还有

<section class="featured-yachts">

    <h2>FEATURED YACHTS</h2>

    <?php 

    $args = array(
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'featured',
                'value' => 'yes',
                'compare' => 'LIKE',
            )
        ),
    );

    $my_posts = new WP_Query($args);

    if ($my_posts->have_posts()) {

        while ($my_posts->have_posts()) : $my_posts->the_post();?>

        <h2><?php the_title(); ?></h2>
        
        <p><?php the_field( 'price' ); ?></p>


        <?php endwhile;
    wp_reset_postdata();

}

    ?>

</section> 

您可以使用 'meta_query',对于 ACF 真假,您不必与 LIKE 进行比较。试试下面的代码。

<section class="featured-yachts">

    <h2>FEATURED YACHTS</h2>
    
    <?php 
    
    $args = array(
        'post_type' => 'yachts',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'featuredyacht',
                'value' => '1',
            )
        ),
    );

    $my_posts = new WP_Query($args);

    if ( $my_posts->have_posts() ) {

        while ( $my_posts->have_posts() ) : $my_posts->the_post();

            echo "Title - ".get_the_title()."</br>";
            echo "Price - ".get_field( "price", get_the_ID() )."</br>";
            echo "Length - ".get_field( "length", get_the_ID() )."</br>";
            echo "Year built - ".get_field( "year_built", get_the_ID() )."</br>";
            echo "Capacity - ".get_field( "capacity", get_the_ID() )."</br>";

        endwhile;

        wp_reset_postdata();

    }
    
    ?>
    
</section> 

要在您的查询中使用多个自定义字段参数,请将 $args 替换为 的答案,如下所示:

$args = array(
    'post_type' => 'yachts',
    'posts_per_page' => -1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'featuredyacht',
            'value' => '1',
        ),
        array(
            'key' => 'price',
            'value' => 50000,
            'type' => 'numeric',
            'compare' => '>=',
        )
    ),
);

以上示例仅获取 yachtsfeaturedyacht 设置为 1trueprice50,000 或更多.根据需要调整以适合您的参数。

例如查询特定价格范围内的特色游艇:

$args = array(
    'post_type' => 'yachts',
    'posts_per_page' => -1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'featuredyacht',
            'value' => '1',
        ),
        array(
            'key' => 'price',
            'value' => array( 50000, 100000 ),
            'type' => 'numeric',
            'compare' => 'BETWEEN',
        )
    ),
);

developer documentation on WP_Query 应该对您进行任何进一步的自定义非常有用。