and*_*ndy 6 sorting wordpress loops
我想基于许多标准创建一个过滤帖子的页面.
我可以wp_query很轻松地处理和发布帖子,我的问题是我无法弄明白(我也无法在网上找到任何关于此的答案,相信我,我看了)如何让用户这样做.
以此为例,按价格(自定义字段元值)从最高到最低返回帖子,共33个帖子.
<?php
$featuredPosts = new WP_Query( array(
'posts_per_page' => 33,
'meta_key'=>'Price',
'orderby' => 'meta_value_num',
'order' => DESC
) );
?>
<?php if ( $featuredPosts->have_posts() ) : ?>
<?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<h2 class="price-title"><?php the_title(); ?> </h2>
</article> <!-- end div post -->
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)
现在,即使在阅读和谷歌搜索之后,如果我能弄清楚如何在前端实现这一点,我会被诅咒,以便用户过滤帖子.
我的意思是,我知道你可以附加到Wordpress中的URL来改变帖子的顺序,但在这种情况下,我完全迷失了.
我试过这个,但它不起作用.
<?php
$by_price = esc_url(add_query_arg(array(
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => ASC
)));
$by_date = esc_url(add_query_arg(array(
'orderby' => 'date',
'order' => DESC
)));
?>
<ul>
<li><a href="<?php echo $by_price;?>">Order by price</a></li>
<li><a href="<?php echo $by_date;?>">Order by date</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我想要实现的目标实际上也非常简单,让用户选择类别,选择价格范围(猜测我在JQuery中写一些东西以便将值传递到字段中),设置他们的结果数量我想回来.
我试过在阳光下搜索一切我能想到的东西,没有骰子.
好的,我更新代码以使其清楚:
---我不认为meta_key会自动拾取---
函数.php
...
$whitList = array(
'price' => array(
'posts_per_page' => 33,
'meta_key'=>'price',
'orderby'=>'meta_value_num',
'order' => ASC
),
'date' => array(
'posts_per_page' => 33,
'orderby'=>'date',
'order' => DESC
)
);
...
Run Code Online (Sandbox Code Playgroud)
你的第一个循环 php:
<?php
gloabl $whitList; //to use the $whitList in your functions.php.
$aryQuery = $whitList[$_REQUEST['orderby']] ? $whitList[$_REQUEST['orderby']] : $whitList['price'];
$featuredPosts = new WP_Query( $aryQuery );
....
....
?>
Run Code Online (Sandbox Code Playgroud)
对于您的列表页面:
<ul>
<?php
gloabl $whitList; //to use the $whitList in your functions.php.
foreach( $whitList as $orderby => $aryOrderBySettings){
?>
<li> <a href="<?php echo esc_url(add_query_arg('orderby', $orderby));?>">Order by <?php echo $orderby;?></a></li>
<?php
}
?>
</ul>
Run Code Online (Sandbox Code Playgroud)