Php 过滤“;自定义帖子类型“;由「;自定义帖子字段“;使用过滤器和;“预先获取帖子”;
我创建了一个名为“公寓”的自定义发布时间 我使用archive-plant.php文件以网格格式循环和显示帖子。 对于循环,我使用“pre_get_posts”操作 这是我在fuctions.php上的“pre_get_posts”操作Php 过滤“;自定义帖子类型“;由「;自定义帖子字段“;使用过滤器和;“预先获取帖子”;,php,wordpress,wordpress-theming,custom-post-type,custom-fields,Php,Wordpress,Wordpress Theming,Custom Post Type,Custom Fields,我创建了一个名为“公寓”的自定义发布时间 我使用archive-plant.php文件以网格格式循环和显示帖子。 对于循环,我使用“pre_get_posts”操作 这是我在fuctions.php上的“pre_get_posts”操作 function post_query($query){ if (!is_admin() && $query->is_main_query()){ if(is_category()){ $qu
function post_query($query){
if (!is_admin() && $query->is_main_query()){
if(is_category()){
$query->set('post_type', array( 'post', 'apartment' ) );
$query -> set('posts_per_page', 3);
}
if( is_post_type_archive( 'apartment' )){
$query -> set('posts_per_page', 8);
}
}
}
add_action('pre_get_posts', 'post_query');
这是我的category.php代码
<?php get_header(); ?>
<div class="container">
<?php
$counter = 1; //start counter
$grids = 3; //Grids per row
if(have_posts()) :
while( have_posts()) : the_post();
?>
<?php
//Show the left hand side column
if($counter == 1) :
?>
<div class="row">
<div class="col-md-4">
<div class="center">
<div class="postimage">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a>
</div>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<h4><?php the_category(' '); ?></h4>
<?php echo get_post_meta($post->ID, 'Price', true); ?>
</div>
</div>
<?php
elseif($counter == 2) :
?>
<div class="col-md-4 border2">
<div class="center">
<div class="postimage">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a>
</div>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<h4><?php the_category(' '); ?></h4>
<?php echo get_post_meta($post->ID, 'Price', true); ?>
</div>
</div>
<?php
elseif($counter == $grids) :
?>
<div class="col-md-4">
<div class="center">
<div class="postimage">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a>
</div>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<h4><?php the_category(' '); ?></h4>
<?php echo get_post_meta($post->ID, 'Price', true); ?>
</div>
</div>
</div>
<div class="clear"></div>
<?php
$counter = 0;
endif;
?>
<?php
$counter++;
endwhile;
?>
<div class="row">
<div class="col-xs-12 text-left ">
<?php next_posts_link('<< Older post'); ?>
</div>
<div class="col-xs-12 text-right ">
<?php previous_posts_link('Newer post >>'); ?>
</div>
</div>
<?php
endif;
?>
</div>
<?php get_footer(); ?>
现在,我想添加过滤器,以便用户可以通过两个自定义字段对自定义帖子类型“公寓”中的帖子进行排序:“价格”和“房间数”
我创建了一个表单并指定了方法=get
<form action="http://localhost/wordpress4/apartment" method="get">
<label>Price:</label>
<select name="Price">
<option value="">Any</option>
<option value="22000">22000</option>
<option value="23000">23000</option>
<option value="24000">24000</option>
<option value="25000">25000</option>
</select>
<label>Rooms:</label>
<select name="Price">
<option value="">Any</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button type="submit" name="">search</button>
</form>
价格:
任何
22000
23000
24000
25000
房间:
任何
2.
3.
搜索
然后我使用下面的代码捕获GET值并将其存储在两个变量中,我将通过pre_GET_posts过滤器在循环中使用这两个变量:
<?php
if($_GET['Price'] && !empty($_GET['Price']))
{
$price = $_GET['Price'];
}
if($_GET['Rooms'] && !empty($_GET['Rooms']))
{
$rooms = $_GET['Rooms'];
}
?>
现在我想知道如何更改功能post\u query($query)-“这是我在pre\u get\u posts中使用的功能”-这样用户可以在自定义post类型url上过滤帖子:localhost/wordpress4/plant/
此外,我希望用户在开始使用过滤器之前(我的意思是当他们第一次访问页面时)查看所有帖子
多谢各位