Sticky posts in search
-
I have been trying to get sticky posts to appear as first posts when they are displayed in search. (im not a coder)
So I am looking for a way to “feature” certain posts and make them appear first in search.
I found this code so far but I have no idea how to implement it.Code:
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; }
Can anyone help me implement this?
and then I would like to be able to order the sticky posts so that for example, 3 out of 8 sticky posts show up as the first 3 hits.My website:
https://www.timbeek.comCan anyone help me out? Any help is greatly appreciated.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Sticky posts in search’ is closed to new replies.