fauzanjeg

Exclude Search Post by Permalink / URL

Apr 8th, 2021 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. /* Exclude Search Post for ajax search */
  2. function custom_search_post( $statement ) {
  3.     /* List Post by Permalink */
  4.     $list_post = array( 'http://localhost/wordpress2/hello-world/', 'http://localhost/wordpress2/shadow-tactics-blades-of-the-shogun-review/' );
  5.  
  6.     foreach( $list_post as $key => $value ) {
  7.         $list_post[$key] = (string) url_to_postid( $value );
  8.     }
  9.  
  10.     $statement['post__not_in'] = $list_post;
  11.  
  12.     return $statement;
  13. }
  14. add_filter( 'jnews_live_search_args', 'custom_search_post' );
  15.  
  16. /* Exclude Search Post */
  17. function wpb_modify_search_query( $query ) {
  18.     global $wp_the_query;
  19.     if( $query === $wp_the_query && $query->is_search() ) {
  20.         /* List Post by Permalink */
  21.         $list_post = array( 'http://localhost/wordpress2/hello-world/', 'http://localhost/wordpress2/shadow-tactics-blades-of-the-shogun-review/' );
  22.  
  23.         foreach( $list_post as $key => $value ) {
  24.             $list_post[$key] = (string) url_to_postid( $value );
  25.         }
  26.  
  27.         $query->set( 'post__not_in', $list_post );
  28.     }
  29. }
  30. add_action( 'pre_get_posts', 'wpb_modify_search_query' );
Add Comment
Please, Sign In to add comment