wordpress - 按自定义字段查询帖子
问题描述
我正在使用出售游艇的高级自定义字段构建一个网站。游艇使用称为“游艇”的自定义 Post 类型</p>
单个游艇列表中的一个字段是一个真/假值,以确定它是否应该是一个特色列表。(它最初是一个复选框,但在尝试了这篇文章中的答案后我改变了它)
我正在尝试在主页上显示精选列表的预览,但无法正常工作。
我最初从 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>
我也试过编辑这个:How filter custom posts by Advanced Custom Fields checkbox
但同样,我无法让它工作。
这些回报中最重要的是h3标题
我无法弄清楚问题是什么。
提前感谢您的帮助
编辑:我至少有一篇使用自定义帖子类型“游艇”的帖子,该帖子类型设置为 true 以表示特色。
我还有很多字段要添加,但我希望这会输出 h3 标题,然后是标记为特色的帖子的标题。
最后,我希望它的功能类似于最新的帖子预览,但仅在“特色”真/假设置为“是”时才显示自定义字段
我创建了一个名为“特色游艇”的 ACF 字段并在 Gutenberg 注册了该块,但其中没有实际的 ACF 字段,它仅用于调用具有此代码的文件“content-featured-yachts-block.php”我正在尝试修复。
我正在尝试使用从各个游艇列表中提取的数据填充此特色游艇块中的帖子预览。在这些列表中是“特色”真/假选项。附上屏幕截图。
这是我注册 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 true 和 false,您不必与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>
推荐阅读
- javascript - 控制台中显示“未定义”的来源
- javascript - 在 React 路由器中动态路由路径问题
- android - 如何在 Kotlin 中扩展自定义视图?
- ngrok - Ngrok 隧道 https 通过配置
- c# - 如何在 Specflow 的同一功能文件中提供多个数据作为输入
- java - Java:如何获得匹配模式的下一次
- sql - SQL Server:子查询返回相同的值
- javascript - 如何从包含对象列表的文件中导出对象并将同一对象导入另一个文件以便能够使用它?
- sql - 计算与先前记录不同的时间
- javascript - 使用绘图板时会触发哪些 JS 事件?