这个让我难过.
我有一个包含此循环的category.php文件:
<?php
if ( have_posts() ) : ?>
<?php
while ( have_posts() ) : the_post(); ?>
<div class="entry-content description clearfix">
<h2 class="category-subtitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php echo the_content(); ?>
<?php global $withcomments; $withcomments = 1;
?>
</div><!-- .entry-content -->
<?php
endwhile;
else :
get_template_part( 'content', 'none' );
endif;
?>
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,并始终返回预期的结果.
另外,在循环之外(在它之后,如果重要的话),我有一个列到这一循环的一侧 - 为了清楚起见,我将把它称为新闻源循环:
<h3 class="newsfeed-heading"><a href="/category/news/">Latest News</a></h3>
<?php
// wp_reset_query(); * Same results with or without wp_reset_query
$args = array(
'cat' => 89,
'order' => 'ASC'
); …Run Code Online (Sandbox Code Playgroud) 在wp_query中,是否可以构建一个复杂的查询,以便我可以通过条件或计算字段进行排序?我想要做的是像MySql中的下一个查询:
SELECT *, field1, field2
case when field1+field2 > some_value then 1 else 2 end as my_alias
FROM my_table
ORDER BY my_alias ASC
Run Code Online (Sandbox Code Playgroud)
我想使用wp_query构建这样的查询,这可能吗?如果是的话,我该怎么做呢?
我认为这很简单,但我不明白.这是我的过滤器:
<form class='post-filters'>
<select name="filter">
<?php
$filter_options = array(
'houses' => 'Houses',
'hotels' => 'Hotels',
);
foreach( $filter_options as $value => $label ) {
echo "<option ".selected( $_GET['filter'], $value )." value='$value'>$label</option>";
}
?>
</select>
<input type='submit' value='Filter!'>
</form>
Run Code Online (Sandbox Code Playgroud)
相关PHP将过滤器应用于wordpress查询:
<?php
global $destinations;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$destinations = new WP_Query([
'paged' => $paged,
'location' => $location,
'category_name' => urldecode(get_query_var('filter')),
'posts_per_page' => 6
]);
?>
Run Code Online (Sandbox Code Playgroud)
如果我选择我的"过滤器"并且结果有六个以上的条目,我next_posts_link()会看到接下来的六个结果.问题是现在,如果我在第2页或第3页,而另一个过滤器少于例如6个条目,我将在更改我的过滤器时看不到任何结果.
如何在更改过滤器时清除get变量(/ page/2 /)?
例:
category/subcategory/subsubcategory/page/3/?filter=houses
Run Code Online (Sandbox Code Playgroud)
现在我选择"过滤"酒店
category/subcategory/subsubcategory/page/3/?filter=hotels
Run Code Online (Sandbox Code Playgroud)
并且"/ page/3"将不会被清除.所以我看不到一些帖子.
在'foreach'循环期间,如果检测到相同的术语,则只添加页面链接,否则在单个wp_query期间添加术语名称和页面链接?
例如:
应该成为:
<?php
$alternate_edition_a = array(
'post_type'=> 'alternate_edition',
'post_status' => 'publish',
'posts_per_page'=>10,
'no_found_rows' => true,
'tax_query' => array(
array(
'taxonomy' => 'game_titles',
'field' => 'slug',
'terms' => $gametitle_con,
)
)
);
$alternate_edition_query = new WP_Query( $alternate_edition_a );
if($alternate_edition_query->have_posts() ) : while ( $alternate_edition_query->have_posts() ) : $alternate_edition_query->the_post();
$alternate_editionTitlesID[] = get_the_ID();
$systemformats_altedition_terms = get_the_terms($post->id, 'system_formats');
foreach ($systemformats_altedition_terms as $systemformats_altedition_term) {
if (!has_term($systemformats_altedition_term->slug, 'system_formats', $gameFeatTitleID)) {
$systemterm_slug[] …Run Code Online (Sandbox Code Playgroud) 问题
我在Wordpress中循环浏览自定义帖子类型(高级自定义字段)。我只想显示start_date等于开始时定义的$ newdate变量的事件。
start_date的格式为YYYY-MM-DD HH:mm(与$ newdate相同)。$ newdate设置为一天的开始,因此我不会排除当天不同时间的事件,并且compare设置为大于(只是为了测试查询)。
但是我没有得到任何结果。
<?php
$newdate = date('Y-m-d 00:00');
//<-- Start the Loop. -->!
$args = array(
'post_type' => 'epsa_events',
'posts_per_page' => 5,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array (
array(
'key' => 'start_time',
'value' => $newdate,
'compare' => '>=',
'type' => 'datetime'
)
)
);
$loop = new WP_Query( $args );
Run Code Online (Sandbox Code Playgroud) 我有两个问题需要尝试合并,以便我的分页工作正常,并且帖子以正确的顺序显示.
我有这个查询:
$today = date('m/d/Y', strtotime('today'));
$args = array(
'post_type' => 'workshops',
"posts_per_page" => 5,
"paged" => $paged,
'meta_key' => 'select_dates_0_workshop_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'select_dates_0_workshop_date',
'meta-value' => "meta_value",
'value' => $today,
'compare' => '>=',
'type' => 'CHAR'
)
)
);
Run Code Online (Sandbox Code Playgroud)
此查询的结果需要在上面的查询之后:
$args = array(
'post_type' => 'workshops',
"posts_per_page" => 5,
"paged" => $paged,
'meta_key' => 'select_dates_0_workshop_date',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'select_dates_0_workshop_date',
'meta-value' => …Run Code Online (Sandbox Code Playgroud) 不确定这是一个奇怪或愚蠢的问题,但我有一个主页滑块的网站设置如下:(显然不是真正的类名,但只是描述它们是什么)
.... WP QUERY, FEATURED POSTS ...
<ul>UPPER SLIDES CONTAINER</ul>
<li class="Slide With Background Image">
<div class="Slide Description">
<span> Category </span>
<h2> Title </h2>
</div>
</li>
....
</ul>
.... SAME WP QUERY, FEATURED POSTS ...
<ul class="Lower Slide Navigation">
<li class="Slide With Background Image">
<div class="Slide Description">
<span> Category </span>
<h2> Title </h2>
</div>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
它以这种方式设置,因为当主要幻灯片在上面循环时,下面的幻灯片显示上一张和下一张即将发布的幻灯片.
无论如何,我的问题是:有没有办法可以使用一个数据库查询来填充这两个区域?
两次拨打同一个电话似乎效率低下......
我正在尝试对wp_query的结果进行排序,我希望通过不同的参数对其进行排序,而无需再次进行查询.我有类似的东西:
$the_query = new WP_Query( $args );
Run Code Online (Sandbox Code Playgroud)
我想要排序$ the_query,WP_Query返回一个这样的结构:
$the_query->posts[0]->title;
Run Code Online (Sandbox Code Playgroud)
所以,我想用'title'对所有元素进行排序.我试过这个:
usort($the_query->posts, function($a, $b) {
return $a['title'] - $b['title'];
});
Run Code Online (Sandbox Code Playgroud)
我想在查询之后进行排序.是因为我想要多次tosort,我不想每次我想要排序时进行查询
解
这将返回致命错误:无法使用WP_Post类型的对象作为数组
usort($the_query->posts, function($a, $b) {
return $a['title'] - $b['title'];
});
Run Code Online (Sandbox Code Playgroud)
这是因为数组的结构是这样的:
$the_query->posts[0]->title;
Run Code Online (Sandbox Code Playgroud)
所以你必须要改变$a['title'] - $b['title'] 的$a->title - $b->title
,用彼得·古森的答案最后的结果是:
usort($the_query->posts, function($a, $b) {
return strcasecmp(
$a->title,
$b->title
);
});
Run Code Online (Sandbox Code Playgroud)
谢谢大家
我创建了一个名为“ snippets”的自定义帖子类型,其中有客户端可以更改的诸如“ address”之类的数据片段。
我创建了一个简单的函数来返回这些片段,但是它不能正常工作,我不确定为什么。
功能:
function get_snippet($snippet_title) {
$snippet_page = new WP_Query(array(
'post_type' => 'snippets',
'post_title' => $snippet_title
));
return $snippet_page->posts[0]->post_content;
}
Run Code Online (Sandbox Code Playgroud)
这是对该函数的示例调用:
echo get_snippet('footer_address');
Run Code Online (Sandbox Code Playgroud)
问题是:
它将始终返回所有代码片段,而不是按post_title过滤。
即使我使用get_posts()它,也将始终以相同的顺序返回所有代码段,并且不会基于post_title返回单个代码段。
结果是等效于:
$snippet_page = new WP_Query(array(
'post_type' => 'snippets'
));
Run Code Online (Sandbox Code Playgroud)
为什么不按“ post_title”过滤?
谢谢
(我也在使用多站点)
这是我的自定义帖子类型初始化代码:
function snippets() {
$labels = array(
'name' => _x( 'Snippets', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Snippet', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Snippets', 'text_domain' ),
'name_admin_bar' => __( …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用WP_Query和一些参数在wordpress中检索一些页面:
$args = array(
'post_type' => 'posttype',
'posts_per_page' => 24,
'post__in' => $store_ids,
'paged' => $paged,
'post_status' => 'publish',
);
$the_query = new WP_Query( $args );
Run Code Online (Sandbox Code Playgroud)
我要在此处检索的页面应与我提供的ID数组中的ID相匹配。数组和其他参数看起来不错,因为当我使用get_posts而不是时确实得到了结果WP_Query。这是怎么了?