WordPress 获取相关文章



  1. 首先获取拥有相同标贴的文章
  2. 如果数量不足,获取拥有部分相同标签的文章
  3. 如果数量还不足,获取拥有相同分类的文章
  4. 再不足,获取部分相同分类的文章


 * class itcGetRelatedPosts
 * get related post by tags and categories
 * $related_posts = new itcGetRelatedPosts($post_id, 3);
 * */

class itcGetRelatedPosts {

	protected $_date;

	public function __construct($post_id, $showposts){
		$this->setDefault($post_id, $showposts);

	protected function setDefault($post_id, $showposts){
		$this->_date['post_id']			= $post_id;
		$this->_date['exclude_ids'][] 	= $post_id;
		$this->_date['showposts']		= $showposts;
		$this->_date['args']['post_type']	= 'post';
		$this->_date['posts']	= array();
		$this->_date['post_count']	= 0;

	protected function _updateArgs(){
		$this->_date['args']['post__not_in']	= $this->_date['exclude_ids'];
		$this->_date['args']['posts_per_page']	= $this->_date['showposts'] - $this->_date['post_count'];

	protected function _getTagIds(){
		$posttags = get_the_tags($this->_date['post_id']);
		if ($posttags) {
			foreach($posttags as $tag) {
				$this->_date['tag_ids'][] = $tag->term_id;
			$this->_date['tag_ids'] = NULL;

	protected function _getCategoryIds(){
		$categories = get_the_category($this->_date['post_id']);
			foreach($categories as $category) {
				$this->_date['category_ids'][] = $category->term_id;
			$this->_date['category_ids'] = NULL;

	protected function _getPosts(){
		if($this->_date['tag_ids'] != NULL){
			//Display posts that are tagged with both tag id 37 and tag id 47
			$this->_date['args']['tag__and'] = $this->_date['tag_ids'];
			//To display posts from either tag id 37 or 47
			if($this->_date['post_count'] < $this->_date['showposts']){
				$this->_date['args']['tag__in'] = $this->_date['tag_ids'];

		if($this->_date['post_count'] < $this->_date['showposts']){
			if(isset($this->_date['args']['tag__and'])) unset($this->_date['args']['tag__and']);
			if(isset($this->_date['args']['tag__in'])) unset($this->_date['args']['tag__in']);
			//Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6:
			$this->_date['args']['category__and'] = $this->_date['category_ids'];
		//To display posts from either category 2 OR 6
		if($this->_date['post_count'] < $this->_date['showposts']){
			$this->_date['args']['category__in'] = $this->_date['category_ids'];

	protected function _pushPost($the_args){
		$the_query = new WP_Query($the_args);
		if ( $the_query->have_posts() ) {
			$this->_date['post_count'] = $this->_date['post_count'] + $the_query->post_count;
			while ( $the_query->have_posts() ) { 
				$this->_date['exclude_ids'][] = get_the_ID();
				$this->_date['posts'][get_the_ID()] = array(
						'ID' => get_the_ID(),
						'title' => get_the_title(),
						'link'	=> get_permalink(),
						//'format'	=> get_label_format(get_the_ID()),
						'excerpt'	=> get_the_excerpt()
				$this->_date['posts'][get_the_ID()]['date'] = get_the_date(__('F j, Y'));

	public function getPosts(){
		return $this->_date['posts'];

	public function get_date(){



2,267 thoughts on “WordPress 获取相关文章

  1. sa

    I appreciate you sharing this blog article. Much obliged.

  2. sa

    I cannot thank you enough for the post.Really looking forward to read more. Cool.

  3. criptomoneda colombiana soda

    Great – I should certainly pronounce, impressed with your web site. I had no trouble navigating through all tabs and related info ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Quite unusual. Is likely to appreciate it for those who add forums or anything, website theme . a tones way for your client to communicate. Nice task..

  4. Christinia Giscombe

    This site is really a stroll-by for all the data you wished about this and didn’t know who to ask. Glimpse here, and you’ll definitely uncover it.

  5. Ross Calendine

    F*ckin’ amazing things here. I am very glad to see your article. Thanks a lot and i’m looking forward to contact you. Will you kindly drop me a e-mail?

  6. Wade Silerio

    I do agree with all the ideas you’ve presented in your post. They are very convincing and will certainly work. Still, the posts are too short for starters. Could you please extend them a bit from next time? Thanks for the post.

  7. Allan Mars

    It’s really a great and helpful piece of info. I am glad that you shared this useful information with us. Please keep us informed like this. Thanks for sharing.

  8. Gita Hirschfeld

    I’m not sure why but this blog is loading incredibly slow for me. Is anyone else having this issue or is it a problem on my end? I’ll check back later and see if the problem still exists.

  9. Romeo Matyas

    Hello There. I discovered your blog using msn. That is an extremely neatly written article. I will be sure to bookmark it and come back to learn more of your useful info. Thanks for the post. I will definitely return.

  10. Leone Memos

    wonderful publish, very informative. I ponder why the other specialists of this sector don’t realize this. You should proceed your writing. I’m sure, you’ve a huge readers’ base already!

Comments are closed.