Wordpress 查询由 acf-field 以标题排序
Wordpress query odererd by acf-field with title
我需要有关 Wordpress 查询的帮助以获取帖子。
我有一个 ACF-numeric-Field 叫做“年”。
我需要一个查询来获取输入年份的帖子,年份作为标题回显。
有点像
1925
- Post一个
- PostB
- PostD
1926
- Post C
- PostE
这是我到目前为止获得的设置“年份”并按其排序的帖子。
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'artikel',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year',
'compare' => 'EXISTS'
),
array(
'key' => 'year',
'value' => 1900,
'type' => 'NUMERIC',
'compare' => '>'
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
谁能帮我如何把年份作为标题?
你可以试试这个:
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'artikel',
'meta_key' => 'year',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
希望能成功!
如果 $the_query
有值,你可以这样做
// assuming that all posts are sorted by year. 1925, 1926, 1930 etc...
$the_query = new WP_Query($args);
// will contain all posts sorted by year
$posts_by_year = [];
// start looping through the posts
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
// get year ACF field
$year = get_field('year');
// check if $posts_by_year already has year, if not create and add the post, if exists just add the post
if (isset($posts_by_year[$year])) {
$posts_by_year[$year][] = get_post();
} else {
$posts_by_year[$year] = [
get_post()
];
}
}
}
wp_reset_postdata();
// check if we have any posts
if (!empty($posts_by_year)) {
foreach ($posts_by_year as $year => $posts) {
echo '<div class="year-block">';
echo '<strong class="year-title">' . $year . '</strong>';
echo '<ul class="posts-by-year">';
foreach ($posts as $post) {
echo '<li>';
echo '<strong class="post-title">' . $post->post_title . '</strong>';
echo '</li>';
}
echo '</ul>';
echo '</div>';
}
}
当然可以根据需要更改 html 格式
我需要有关 Wordpress 查询的帮助以获取帖子。
我有一个 ACF-numeric-Field 叫做“年”。 我需要一个查询来获取输入年份的帖子,年份作为标题回显。
有点像
1925
- Post一个
- PostB
- PostD
1926
- Post C
- PostE
这是我到目前为止获得的设置“年份”并按其排序的帖子。
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'artikel',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year',
'compare' => 'EXISTS'
),
array(
'key' => 'year',
'value' => 1900,
'type' => 'NUMERIC',
'compare' => '>'
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
谁能帮我如何把年份作为标题?
你可以试试这个:
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'artikel',
'meta_key' => 'year',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
希望能成功!
如果 $the_query
有值,你可以这样做
// assuming that all posts are sorted by year. 1925, 1926, 1930 etc...
$the_query = new WP_Query($args);
// will contain all posts sorted by year
$posts_by_year = [];
// start looping through the posts
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
// get year ACF field
$year = get_field('year');
// check if $posts_by_year already has year, if not create and add the post, if exists just add the post
if (isset($posts_by_year[$year])) {
$posts_by_year[$year][] = get_post();
} else {
$posts_by_year[$year] = [
get_post()
];
}
}
}
wp_reset_postdata();
// check if we have any posts
if (!empty($posts_by_year)) {
foreach ($posts_by_year as $year => $posts) {
echo '<div class="year-block">';
echo '<strong class="year-title">' . $year . '</strong>';
echo '<ul class="posts-by-year">';
foreach ($posts as $post) {
echo '<li>';
echo '<strong class="post-title">' . $post->post_title . '</strong>';
echo '</li>';
}
echo '</ul>';
echo '</div>';
}
}
当然可以根据需要更改 html 格式