• Resolved clarkdprice

    (@clarkdprice)


    Hi,

    I’m having an issue removing some specific pages from my recommendations. Here is my function:

    //Add the post ids of posts or pages you do not want to appear in the recommendations
    
    function jetpackme_exclude_related_post( $exclude_post_ids, $post_id ) {
        // $post_id is the post we are currently getting related posts for
        $exclude_post_ids[] = 6956; // Exclude Welcome to Jacksonville Distance Presentation
        $exclude_post_ids[] = 6954; // Welcome to TWS Distance Presentation
        $exclude_post_ids[] = 6959; // Welcome to TWSTC Distance Presentation 
    
        return $exclude_post_ids;
    }
    add_filter( 'jetpack_relatedposts_filter_exclude_post_ids', 'jetpackme_exclude_related_post', 20, 3 );

    I’m also using the rawcode and building out the recommendations on these pages, not sure if that affects anything. I’ve double checked the post ids from the urls, not sure what else to.

    Thanks guys!

    https://www.ads-software.com/plugins/jetpack/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Could you post some links to the posts where you’re experiencing the issue, so I can take a closer look?

    If you want your site URL to remain private, you can also contact us via this contact form:
    https://jetpack.me/contact-support/

    I’m also using the rawcode and building out the recommendations on these pages

    Could you tell me more about this, and paste the code you’re using here, if you think it could affect the way Related Posts are displayed?

    Thanks!

    Thread Starter clarkdprice

    (@clarkdprice)

    Sure. The this https://weldingschool.com

    The pages that I am trying to remove are below, post ids can me seen in my OP:

    https://www.weldingschool.com/twsh/
    https://www.weldingschool.com/twsj/
    https://www.weldingschool.com/twst/

    Here is the rawcode for the recommendation engine

    /* Create shortcode for displaying related posts anywhere in the post */
    function labnol_related_shortcode( $atts ) {
    
        $related_posts = "";
    
        if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) {
    
            $related = Jetpack_RelatedPosts::init_raw()
                ->set_query_name( 'jetpackme-shortcode' )
                ->get_for_post_id(
                    get_the_ID(),
                    array( 'size' => 3 ) // How many related posts?
                );
    
            if ( $related ) {
    
                foreach ( $related as $result ) {
                    $related_post = get_post( $result[ 'id' ] );
                    $url = get_permalink($related_post->ID);
                    $title = $related_post->post_title;
                    $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $related_post->ID ), 'single-post-thumbnail' );   
    
                  		if ($thumbnail != '') {
                  			$related_posts .= "<li><a href='" . $url . "'><figure style='height: 111px; background-image: url(". $thumbnail[0] ."); background-size: cover;' class='full-image attachment-related-small' scale='0'></figure></a><h4><a class='related-link' href='" . $url . "'>$title</a></h4></li>";
    
                  		} else {
                  			$related_posts .= "<li><a href='" . $url . "'><figure style='height: 111px; background-image: url(/wp-content/uploads/2015/02/banner-weld7.jpg); background-size: cover;' class='full-image attachment-related-small' scale='0'></figure></a><h4><a class='related-link' href='" . $url . "'>$title</a></h4></li>";
                  		}
    
                }
                $related_posts = '<ul>' . $related_posts . '</ull>';
            }
    
        }
    
        return $related_posts;
    }
    
    /* Create a new shortcode for Jetpack related posts */
    add_shortcode( 'labnol_related', 'labnol_related_shortcode' );

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Thanks for the extra details!

    The raw method offers far less filters than the built-in solution. That class is in fact very “raw”, it only includes one filter, jetpack_relatedposts_filter_hits:
    https://github.com/Automattic/jetpack/blob/3.8.1/modules/related-posts/jetpack-related-posts.php#L1266

    jetpack_relatedposts_filter_exclude_post_ids is only applied to the main class, so these posts won’t be shown at the bottom of your regular blog posts:
    https://github.com/Automattic/jetpack/blob/3.8.1/modules/related-posts/jetpack-related-posts.php#L670

    To implement something similar to jetpack_relatedposts_filter_exclude_post_ids in your custom implementation, I’d recommend getting 6 related posts every time instead of 3, and remove any post that would match one of the post IDs you want to exclude. This way, you’ll always have at least 3 results. You can then make sure your loop returning posts stops after 3 occurrences.

    I hope this helps.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Removing specific related posts not working :(’ is closed to new replies.