• Resolved Gus

    (@rolliespoke)


    I’m confused on where I would put the post ID in order to make this work. It could be a syntax thing, but I would appreciate if someone could actually show me an example using the ID’s below

    My post ID is 11170
    I would like to this post ID -> 11230 to appear in the related posts plugin.

    Where would I insert the two ID’s in the example below?

    function jetpackme_append_related_post( $hits, $post_id ) {
    // $post_id is the post we are currently getting related posts for
    // Add 1036 to the front of the hits array
    array_unshift( $hits, array( ‘id’ => 1036 ) );
    return $hits;
    }
    add_filter( ‘jetpack_relatedposts_filter_hits’, ‘jetpackme_append_related_post’ );

    https://wordpress.org/plugins/jetpack/

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

    (@jeherve)

    Jetpack Mechanic 🚀

    Try the following:

    function jetpackme_append_related_post( $hits, $post_id ) {
    	// $post_id is the post we are currently getting related posts for
            if ( 11170 == $post_id ) {
        	    // Add 1036 to the front of the hits array
    	    array_unshift( $hits, array( 'id' => 11230 ) );
                // Remove the last element of the array
                array_pop( $hits );
            }
    
    	return $hits;
    }
    add_filter( 'jetpack_relatedposts_filter_hits', 'jetpackme_append_related_post', 20, 2 );
    Thread Starter Gus

    (@rolliespoke)

    Thanks for your reply. Using this code didn’t change anything.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    Could you post a link to that post so I can take a look?

    If you want it to remain private, you can also contact us via this contact form:
    http://jetpack.me/contact-support/

    Thread Starter Gus

    (@rolliespoke)

    Sure, here is a link to a post http://rollingspoke.com/pedal-mode-monday-vol-16/

    The post ID in the link is -> 10997
    The related post I’m looking to have appear -> 8329

    I have the following in the functions.php

    function jetpackme_append_related_post( $hits, $post_id ) {
    	// $post_id is the post we are currently getting related posts for
           if ( 10997 == $post_id ) {
       	    // Add 1036 to the front of the hits array
    	    array_unshift( $hits, array( 'id' => 8329 ) );
               // Remove the last element of the array
               array_pop( $hits );
           }
    
    	return $hits;
    }
    add_filter( 'jetpack_relatedposts_filter_hits', 'jetpackme_append_related_post', 20, 2 );
    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    That code is correct. Could you try to add it via a plugin instead, like this:
    http://i.wpne.ws/ZJzm

    Let me know how it goes.

    Thread Starter Gus

    (@rolliespoke)

    Thanks for the zip file, it worked for this particular post. But what if I want to do the same for other posts?

    I duplicated the code in the plugin and just changed the ID’s to correspond to a different post and related post, but that didn’t produce any results.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    If you want to add more posts, you can indeed duplicate the function in the plugin and change the post IDs. It’s worth noting that you’ll have to change the function name as well, in both places where it’s displayed (when declaring the function and when hooking it into jetpack_relatedposts_filter_hits).

    Thread Starter Gus

    (@rolliespoke)

    Oh I missed the change when hooking into jetpack_relatedposts_filter_hits.

    Everything works great now, thanks so much for you help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Replace one of the Related Posts by a custom result’ is closed to new replies.