• I’m modifying a function that gets posts related by cat and tag to the current post. I’m having trouble after modifying it’s still not showing what I want.

    Original

    public static function get_related_posts($max_posts = 20){
    		$my_posts = array();
    		
    		$post_id = get_the_ID();
    		
    		$tags_string = '';
    		$post_tags = get_the_tags();
    		if ($post_tags) {
    			foreach ($post_tags as $post_tag) {
    				$tags_string .= $post_tag->slug . ',';
    			}
    		}
    		
    		$query = array(
    						'exclude' => $post_id,
    						'numberposts' => $max_posts,
    						'tag' => $tags_string
    					 );
    					 
    		$get_relateds = apply_filters('essgrid_get_related_posts', $query, $post_id);
    		$tag_related_posts = get_posts($get_relateds);		
    		
    		
    		if(count($tag_related_posts) < $max_posts){
    			$ignore = array();
    			foreach($tag_related_posts as $tag_related_post){
    				$ignore[] = $tag_related_post->ID;
    			}
    			$article_categories = get_the_category($post_id);
    			$category_string = '';
    			foreach($article_categories as $category) { 
    				$category_string .= $category->cat_ID . ',';
    			}
    			$max = $max_posts - count($tag_related_posts);
    			
    			$excl = implode(',', $ignore);
    			$query = array(
    							'exclude' => $excl,
    							'numberposts' => $max,
    							'category' => $category_string
    						 );
    						 
    			$get_relateds = apply_filters('essgrid_get_related_posts_query', $query, $post_id);
    			$cat_related_posts = get_posts($get_relateds);
    			
    			$tag_related_posts = $tag_related_posts + $cat_related_posts;
    		}
    		
    		foreach($tag_related_posts as $post){
    		
    			$the_post = array();
    			
    			if(method_exists($post, "to_array"))
    				$the_post = $post->to_array();
    			else
    				$the_post = (array)$post;
    			
    			if($the_post['ID'] == $post_id) continue;
    			
    			$my_posts[] = $the_post;
    		}
    		
    		return apply_filters('essgrid_get_related_posts', $my_posts);
    	}
    

    stripping out all tag related code

    
    	public static function get_related_posts($max = 20){
    		$my_posts = array();
    		
    		$post_id = get_the_ID();
    		
    		$article_categories = get_the_category($post_id);
    		$category_string = '';
    		foreach($article_categories as $category) { 
    			$category_string .= $category->cat_ID . ',';
    		}
    		
    		$query = array(
    						'exclude' => $post_id,
    						'numberposts' => $max,
    						'category' => $category_string
    					 );
    					 
    		$get_relateds = apply_filters('essgrid_get_related_posts_query', $query, $post_id);
    		$cat_related_posts = get_posts($get_relateds);
    			
    		
    		foreach($cat_related_posts as $post){
    		
    			$the_post = array();
    			
    			if(method_exists($post, "to_array"))
    				$the_post = $post->to_array();
    			else
    				$the_post = (array)$post;
    			
    			if($the_post['ID'] == $post_id) continue;
    			
    			$my_posts[] = $the_post;
    		}
    		
    		return apply_filters('essgrid_get_related_posts', $my_posts);
    	}
    

    But I’m getting everything returned. I’m not a dev. I don’t even know how to start debuggin. Do you see what’s wrong?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter funsail

    (@funsail)

    Btw I couldn’t find where essgrid_get_related_posts/essgrid_get_related_posts_query was defined in the while plugin
    I don’t know what that means.

    Moderator bcworkz

    (@bcworkz)

    The applied filters possibly do nothing and are provided to allow other devs to alter the query arguments or resulting posts. Using these hooks is much preferable to directly altering plugin code. When the plugin is updated, your modifications will be overwritten. By using filter hooks from your own plugin or child theme, your code will be safe from updates.

    You can use the ‘essgrid_get_related_posts_query’ to inject your own query arguments. The ‘essgrid_get_related_posts’ filter can be used to completely override the original function and replace the default results with your own. Once you figure out why your code is not working, I suggest you convert your code into a callback function declared and hooked on your own site specific plugin (which can contain any other custom code as well) and restore the original plugin code.

    Whether you do as suggested or not is your decision, either way you need working code. Debugging is mainly a process of checking intermediate results to be sure the values are as expected. Of significant assistance is defining WP_DEBUG as true in wp-config.php. Address any PHP messages displayed, even if they are mere notices and the code otherwise works.

    Often you can check intermediate values by using echo, var_dump(), or print_r() to output variable values. echo is fine for single values, use the others for arrays and objects. In some cases direct output will not display on the browser page. If that’s the case, you need to find some other way to output debug data. In this case, you could define a dummy “related” post that contains debug output and include it in the returned array. The related post output on the template will need to display post content directly from the returned array since this dummy post does not actually exist in the DB. You may need to temporarily alter the template to do this. Try directly outputting debug data first, it’s clearly much easier if you can do it.

    Some things to check first: Be sure $get_relateds == $query, if so it means there is no hook to ‘essgrid_get_related_posts_query’ that changes your query arguments. (BTW, remove these apply_filters() calls to callback functions hooked into the same or you will create an infinite loop situation) Verify $cat_related_posts contains posts. If not, the query is failing for some reason.

    If those check out, the problem is later code. Go through the steps one at a time until you locate the problem. If things already do not check out, there’s a problem prior. Start at the beginning and go through the steps one at a time. Does $post_id have a valid post ID? If not, this code must be running outside the loop. Does $article_categories contain an array of category objects? Etc., etc. until you find the problem. Good luck in your bug hunt!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Making relatd posts’ is closed to new replies.