• Hi All

    Here is how to make Sticky Posts in Relevanssi, comments and improvements are welcome

    add_action( ‘admin_init’, ‘createCustomFields’ ,1 );
    // add the custum metabox to post type
    // can do the same to pages or other custom types
    // can also loop throughout all types to add this snippet
    function createCustomFields() {
    add_meta_box( ‘sticky-post’, ‘Sticky Post’ , ‘displayStickyPostsMetaBox’, ‘post’, ‘normal’, ‘high’ );
    }

    // Lets make sure we save it
    add_action( ‘save_post’, ‘saveCustomFields’ , 1);
    function saveCustomFields()
    {
    global $post;
    $id = $post->ID;
    if ( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE ) {
    return $id;
    }

    saveCustomField(“MakePostSticky”);
    }

    // Lets display the Sticky Post Snippet
    function displayStickyPostsMetaBox()
    {

    displayDivPrefix(“Make this post sticky in Relenanssi Search Results”);
    displayDropBoxEx(“MakePostSticky”,”Make Post Sticky”,”Yes,No”,”Yes,No”, “No”);
    displayDivSuffix();

    }

    // Relevansi hit filter – customization of search results:
    // pop up sticky posts
    add_filter( ‘relevanssi_hits_filter’, ‘my_stickyposts_hits_filter’ );

    function my_stickyposts_hits_filter( $data ) {

    $hits = $data[0];
    $regularposts = array();
    $stickyposts = array();

    foreach ( $hits as $key => $hit ) {

    $MakePostSticky = get_post_meta( $hit->ID, “MakePostSticky”, true );

    if ( $MakePostSticky == “Yes” ) $stickyposts[] = $hit;
    else $regularposts[] = $hit;

    }

    // we are placing the sticky posts on top in the same order thet appears in
    // search results and the rest goes after them
    $filtered_hits = array_merge($stickyposts, $regularposts);
    $filtered_data = array( $filtered_hits, $data[1] );

    return $filtered_data;
    }

    David

    http://ww.wp.xz.cn/extend/plugins/relevanssi/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘[Plugin: Relevanssi – A Better Search] Sticky Posts’ is closed to new replies.