WordPress 循环忽略三个单独 meta_values 中的第一个 post
WordPress loop ignore first post of three separate meta_values
我有一个循环可以提取所有新闻,但是 ACF 设置了三个主要故事。这些是主要的、次要的和第三个。如果每个字段只有一个 post 设置,这将不是问题。但是,客户希望能够只设置一个新的 Main post 而不必担心删除旧的。
因此,为了完成这项工作,我试图让循环忽略这三个字段中的第一个,同时显示其余字段和设置为 'No' 的其他 post。
我正在尝试类似的操作,但我看不出还有什么方法可以做到。
$args = array(
// 'offset' => 1,
'posts_per_page' => -1,
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Secondary',
'compare' => 'NOT',
)
),
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Third',
'compare' => 'NOT',
)
),
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Main',
'compare' => 'NOT',
)
),
);
我知道偏移删除了重要的分页功能,但我看到了 https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination 并且还被告知了一种解决此问题的方法。这部分暂时比较重要。
以下是我最终如何克服无法执行上述操作的方法
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php
$excluded_key = "main_story";
$excluded_val = array("Main", "Secondary", "Third");
$exclude_ids = array();
?>
<?php
foreach ($excluded_val as $exclude) {
$args = array(
'posts_per_page' => 1,
'order' => 'DESC',
'meta_query' => array(
array(
'key' => $excluded_key,
'value' => $exclude,
)
)
);
$excluded_id = get_posts($args);
foreach($excluded_id as $to_exclude) {
$exclude_ids[] = $to_exclude->ID;
}
}
?>
<?php
$args = array(
'post__not_in' => $exclude_ids,
'paged' => $paged
);
?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
我有一个循环可以提取所有新闻,但是 ACF 设置了三个主要故事。这些是主要的、次要的和第三个。如果每个字段只有一个 post 设置,这将不是问题。但是,客户希望能够只设置一个新的 Main post 而不必担心删除旧的。
因此,为了完成这项工作,我试图让循环忽略这三个字段中的第一个,同时显示其余字段和设置为 'No' 的其他 post。
我正在尝试类似的操作,但我看不出还有什么方法可以做到。
$args = array(
// 'offset' => 1,
'posts_per_page' => -1,
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Secondary',
'compare' => 'NOT',
)
),
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Third',
'compare' => 'NOT',
)
),
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Main',
'compare' => 'NOT',
)
),
);
我知道偏移删除了重要的分页功能,但我看到了 https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination 并且还被告知了一种解决此问题的方法。这部分暂时比较重要。
以下是我最终如何克服无法执行上述操作的方法
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php
$excluded_key = "main_story";
$excluded_val = array("Main", "Secondary", "Third");
$exclude_ids = array();
?>
<?php
foreach ($excluded_val as $exclude) {
$args = array(
'posts_per_page' => 1,
'order' => 'DESC',
'meta_query' => array(
array(
'key' => $excluded_key,
'value' => $exclude,
)
)
);
$excluded_id = get_posts($args);
foreach($excluded_id as $to_exclude) {
$exclude_ids[] = $to_exclude->ID;
}
}
?>
<?php
$args = array(
'post__not_in' => $exclude_ids,
'paged' => $paged
);
?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>