wordpress中WP_Query使用post_title做条件

作者:stoat 发布时间:2014-12-31 分类:wordpress

wordpress中WP_Query使用post_title做条件


$args = array(
		'post_type' => 'brand',
		'post_title' => $brand,
		's' => $brand,
		'post_status' => 'publish',
		'posts_per_page' => 1 // you may edit this number
	);
	$loop = new WP_Query($args);
	print_r($loop);

打印sql为:

[request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND (((wp_posts.post_title LIKE '%技术沙龙%') OR (wp_posts.post_content LIKE '%技术沙龙%')))  AND wp_posts.post_type = 'brand' AND (wp_posts.post_status = 'publish')  ORDER BY wp_posts.post_date DESC LIMIT 0, 1

wordpress文章状态码


publish – 已发布
pending – 审核中
draft – 草稿
auto-draft – 自动草稿
future – 定时发布
private – 私有
inherit – 修订版(自动存档)
trash – 回收站
any – 任何

附WP_Query使用条件


//按作者 $query = new WP_Query( 'author_name=admin' ); $query = new WP_Query( 'author=1,2,3' ); //过滤某个作者 $query = new WP_Query( 'author=-1' ); //按 Category $query = new WP_Query( 'cat=1,2,4' );//包含子类 $query = new WP_Query( 'category_name=category' ); //过滤掉某分类 $query = new WP_Query( 'cat=-1,-3' ); //不属于某分类下的文字 $query = new WP_Query( array( 'category__not_in' => array( 1, 4) ) ); //同时属于分类2,4 $query = new WP_Query( array( 'category__and' => array( 2, 4) ) ); //属于分类2,4 $query = new WP_Query( array( 'category__in' => array( 2, 4) ) );//不包含子类 //按 Tag $query = new WP_Query( 'tag=tag1,tag2' ); $query = new WP_Query( 'tag_id=1' ); //同时包含有指定标签的文章合集 $query = new WP_Query( 'tag=tag1+tag2+tag3' ); //调用标签ID为2和4的文章 $query = new WP_Query( array( 'tag__and' => array( 2, 4 ) ) ); //调用标签ID为2或4的文章 $query = new WP_Query( array( 'tag__in' => array( 2, 4 ) ) ); //过滤掉标签ID为2或4的文章 $query = new WP_Query( array( 'tag__not_in' => array( 2, 4 ) ) ); //按 Post 和 Page 调用文章 //指定文章ID $query = new WP_Query( 'p=11' ); //指定页面的ID query = new WP_Query( 'page_id=12' ); //指定文章别名 $query = new WP_Query( 'name=the-article-name' ); //指定页面别名 $query = new WP_Query( 'pagename=about' ); //页面url层级调用 $query = new WP_Query( 'pagename=about/contact' ); //按ID和文章属性 $query = new WP_Query( array( 'post_type' => 'page', 'post__in' => array( 2, 5 ) ) ); //按ID过滤页面 $query = new WP_Query( array( 'post__not_in' => array( 2, 5, 20 ) ) ); //按属性 //全部页面 $query = new WP_Query( 'post_type=page' ); //自定义文章 $query = new WP_Query( array( 'post_type' => array( 'movie', 'book' ) ) ); //状态调用 $query = new WP_Query( 'post_status=post' ); //多重调用 $query = new WP_Query( array( 'post_status' => array( 'pending', 'draft', 'future' ) ) ); //调用附件 $query = new WP_Query( array( 'post_status' => 'any', 'post_type' => 'attachment' ) ); //按分页调用 $query = new WP_Query( 'posts_per_page=15' ); //显示所有文章 $query = new WP_Query( 'posts_per_page=-1' ); //显示第4页的文章 $query = new WP_Query( 'paged=4' ); //当前页文章 $query = new WP_Query( 'paged=' . get_query_var( 'paged' ) ); //当前页并设置默认第一页 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $query = new WP_Query( 'paged=' . $paged ); //文章过滤 //过滤掉前3篇文章 $query = new WP_Query( 'offset=3' ) ); //每5篇文章分页一次,并且过滤掉第一篇文章 $query = new WP_Query( array( 'posts_per_page' => 5, 'offset' => 1 ) ); //文章排序 //按文章名倒序排列 $query = new WP_Query( array ( 'orderby' => 'title', 'order' => 'DESC' ) ); //随机显示5篇文章 $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '5' ) ); //按评论数量排列 $query = new WP_Query( array( 'orderby' => 'comment_count' ) ); //置顶文章 //显示第一篇置顶文章 $sticky = get_option( 'sticky_posts' ); $query = new WP_Query( 'p=' . $sticky[0] ); //显示第一篇置顶文章,如果没有置顶文章则返回到最新的文章 $args = array( 'posts_per_page' => 1, 'post__in' => get_option( 'sticky_posts' ), 'ignore_sticky_posts' => 1 ); $query = new WP_Query( $args ); //显示第一篇置顶文章,如果没有置顶文章则不显示任何内容 $sticky = get_option( 'sticky_posts' ); $args = array( 'posts_per_page' => 1, 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ); $query = new WP_Query( $args ); if ( $sticky[0] ) { //code } //不显示置顶文章 $query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) ); //显示分类ID为6的分类下所有文章,并且每3篇分一页,不让置顶文章显示在最顶部 $query = new WP_Query( 'ignore_sticky_posts=1&posts_per_page=3&cat=6' ); //排除置顶文章,返回到分类最新文章,按系统分页规则分页 $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; $sticky = get_option( 'sticky_posts' ); $args = array( 'cat' => 3, 'ignore_sticky_posts' => 1, 'post__not_in' => $sticky, 'paged' => $paged ); $query = new WP_Query( $args ); //自定义文章类型 //显示自定义产品文章类型中,自定义属性color非蓝或20<price<100的产品集合 $args = array( 'post_type' => 'product', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'color', 'value' => 'blue', 'compare' => 'NOT LIKE' ), array( 'key' => 'price', 'value' => array( 20, 100 ), 'type' => 'numeric', 'compare' => 'BETWEEN' ) ) ); $query = new WP_Query( $args ); //自定义属性为color,值不为red的文章集合 $query = new WP_Query( array( 'meta_key' => 'color', 'meta_value' => 'red' , 'meta_compare' => '!=' ) ); 根据分类名称获得分类ID: get_cat_ID('分类名称');

原文地址:wordpress中WP_Query使用post_title做条件 by 雪鼬博客

标签:wordpress

评论已关闭