如果你query要设置一篇为特色文章放大,其他几篇放小的话,如何设置才能不重复单独放大一篇呢,要用到wp query的属性之一,post__not_in. 你可以在第一个个query里面设置下读取他的id,然后在第二个query里设置下post__not_in 这个id的post. 基本的query格式为,
<section class="container">
<article class="main">
<?php
$query = new WP_Query(array(
'posts_per_page' => 1,
'post_type' => 'post'
));
while($query->have_posts()) : $query->the_post(); $nowshowid = $post->ID;
?>
<div class="featured">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<ul>
<?php
$query2 = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 20,
'orderby' => 'comment_count',
'order' => 'DESC',
'post__not_in' => [$nowshowid]
));
while ($query2->have_posts()) {
$query2->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</li>
<?php
} wp_reset_postdata(); ?>
</ul>
</article>
</section>
如果要按找自定义字段进行排序,怎么办?这里比方说我们每个帖子里有个排序的自定义字段,内容都是数字。怎样设置下才能按照这个数字进行排序?这里就要用到自定义排序的方式,很重要的一个方式就是按找meta_value 排序,也就是’orderby’ => ‘meta_value’, 之前要申明meta_key的数值,也就是自定义字段order.
所有$post变量带的数据都形成了post的元数据,也就是meta_value, 而meta_key就是元数据的查询方法。如果以自定义字段order作为排序的写法就是:
$query2 = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 20,
'meta_key' => 'order',
'orderby' => 'meta_value',
'order' => 'ASC',
'post__not_in' => [$nowshowid]
));
while ($query2->have_posts()) {
$query2->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</li>
<?php
} wp_reset_postdata(); ?>
注意,如果用做数字的排序的话,排序的orderby可以写成meta_value_num。专门数字排序的方法。
如果要进行data比较排序的话,要用到高级的排序方法。
wp query有两种高级的排序方法,一种是meta_query,用到的是自定义字段的高级排序,一种是tax_query,排序的是分类形式。
meta_query是查询和比较多个字段。只有一个字段的query的方式如下:
<?php
$query2 = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 20,
'orderby' => 'meta_value',
'meta_key' => 'order',
'meta_compare' => '<=',
'meta_value' => 5,
//'order' => 'ASC',
//'post__not_in' => [$nowshowid]
));
while ($query2->have_posts()) {
$query2->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</li>
<?php
这个query就查询了字段名称为order,数值小于等于5的所有帖子。注意每个项目的用法。
meta_query写法:
$query2 = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 20,
'meta_query' => array(
'relation' => 'OR', //默认是AND
array(
'key' => 'order',
'value' => '80',
'type' => 'CHAR',
'compare' => '<='
),
array(
'key' => 'price',
'value' => 100,
'type' => 'NUMERIC',
'compare' => '<'
)
),
'post__not_in' => [$nowshowid]
));
while ($query2->have_posts()) {
$query2->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</li>
<?php
} wp_reset_postdata();
如果要查询自定义字段区间在1-6之间的文章,可以用到compare的between方法,如下:
'meta_query' => array(
array(
'key' => 'order',
'value' => array(1,3),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
),
meta query可以内嵌的,比方说你要先查询一个逻辑,颜色是绿色,尺寸是大号的,这个结果当作A, 你要同时查询一个结果, 编号是<5的,结果是B,你要A结果和B结果一起查询,所以A结果你要先写一个array,写上relation, 然后再写两个array, 进行查询。
category参数、tags参数的用法:
// Category Parameters - Show posts associated with certain categories.
// http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
'cat' => 5, // (int) - Display posts that have this category (and any children of that category), using category id.
'cat' => '-12,-34,-56' // Display all posts except those from a category by prefixing its id with a '-' (minus) sign.
'category_name' => 'staff, news', // (string) - Display posts that have these categories (and any children of that category), using category slug.
'category_name' => 'staff+news', // (string) - Display posts that have "all" of these categories, using category slug.
'category__and' => array( 2, 6 ), // (array) - Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6.
'category__in' => array( 2, 6 ), // (array) - Display posts that have this category (not children of that category), using category id.
'category__not_in' => array( 2, 6 ), // (array) - Display posts that DO NOT HAVE these categories (not children of that category), using category id.
// Tag Parameters - Show posts associated with certain tags.
// http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
'tag' => 'cooking', // (string) - use tag slug.
'tag_id' => 5, // (int) - use tag id.
'tag__and' => array( 2, 6), // (array) - use tag ids.
'tag__in' => array( 2, 6), // (array) - use tag ids.
'tag__not_in' => array( 2, 6), // (array) - use tag ids.
'tag_slug__and' => array( 'red', 'blue'), // (array) - use tag slugs.
'tag_slug__in' => array( 'red', 'blue'), // (array) - use tag slugs.
tax_query的用法:
// Taxonomy Parameters - Show posts associated with certain taxonomy.
// http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
// Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays)
// This construct allows you to query multiple taxonomies by using the relation parameter in the first (outer) array to describe the boolean relationship between the taxonomy queries.
'tax_query' => array( // (array) - use taxonomy parameters (available with Version 3.1).
'relation' => 'AND', // (string) - The logical relationship between each inner taxonomy array when there is more than one. Possible values are 'AND', 'OR'. Do not use with a single inner taxonomy array. Default value is 'AND'.
array(
'taxonomy' => 'color', // (string) - Taxonomy.
'field' => 'slug', // (string) - Select taxonomy term by Possible values are 'term_id', 'name', 'slug' or 'term_taxonomy_id'. Default value is 'term_id'.
'terms' => array( 'red', 'blue' ), // (int/string/array) - Taxonomy term(s).
'include_children' => true, // (bool) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
'operator' => 'IN' // (string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND', 'EXISTS' and 'NOT EXISTS'. Default value is 'IN'.
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array( 103, 115, 206 ),
'include_children' => false,
'operator' => 'NOT IN'
)
),
自定义筛选的样例:
<?php
if ($_GET['minprice'] && !empty($_GET['minprice'])) {
$minprice = $_GET['minprice'];
} else {
$minprice = 0;
}
if ($_GET['maxprice'] && !empty($_GET['maxprice'])) {
$maxprice = $_GET['maxprice'];
} else {
$maxprice = 99999;
}
if ($_GET['size'] && !empty($_GET['size'])) {
$size = $_GET['size'];
}
if ($_GET['color'] && !empty($_GET['color'])) {
$color = $_GET['color'];
}
echo $minprice . $maxprice . $size . $color;
?>
<div class="filter">
<h1>Category filter</h1>
<div class="container">
<form action="<?php echo site_url('/') ?>" method="get">
<label>min:</label>
<input type="number" name="minprice" <?php echo $minprice; ?>>
<label>max:</label>
<input type="number" name="maxprice" <?php echo $maxprice; ?>>
<label>Size:</label>
<select name="size" id="">
<option value="">ANY</option>
<option value="s">S</option>
<option value="m">M</option>
<option value="l">L</option>
<option value="xl">XL</option>
</select>
<label>Color:</label>
<select name="color" id="">
<option value="">ANY</option>
<option value="red">RED</option>
<option value="blue">BLUE</option>
<option value="pink">PINKL</option>
<option value="beige">BEIGE</option>
</select>
<button type="submit" name="">FILTER</button>
</form>
</div>
</div>
<div class="category">
<strong>Tags:</strong>
<?php
$catpost = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'price',
'type' => 'NUMERIC',
'value' => array($minprice,$maxprice),
'compare' => 'BETWEEN'
),
array(
'key' => 'size',
'value' => $size,
'compare' => 'LIKE' //用like原因是当size没有属性的时候就会通配所有,因为我们设置了$size必须设置不空的时候有值。 作为通配符而存在。 比方说设置value作为 bl的话,compare设置为LIKE, 就会统配bl*类型 也就是数据库存在的blue.
)),
array(
'key' => 'color',
'value' => $color,
'compare' => 'LIKE' //用like原因是当size没有属性的时候就会通配所有,因为我们设置了$size必须设置不空的时候有值。 作为通配符而存在。 比方说设置value作为 bl的话,compare设置为LIKE, 就会统配bl*类型 也就是数据库存在的blue.
)),
);
while($catpost->have_posts()) : $catpost->the_post();
?>
<h3><?php the_category(); ?></h3>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php the_tags( ); ?>
<h2 style="color: red">Price:<?php the_field('price') ?></h2>
<?php endwhile; ?>
</div>
select设置选中结果的话要设置options selected属性为selectd.
<option selected="selected">
3
</option>