Advertisement
Guest User

kitten

a guest
Oct 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1. <?php
  2.    
  3. // Remove single listings full page for basic users.
  4. function fq_disable_single_cpt_views() {
  5.     $queried_post_type = get_query_var('post_type');
  6.     $cpts_without_single_views = array( 'business' );
  7.     $featured_listing = get_post_meta(get_the_ID(), "featured_listing", TRUE);
  8.     if (    is_single()
  9.         &&  in_array( $queried_post_type, $cpts_without_single_views )
  10.         &&  ("" === $featured_listing)
  11.     ) {
  12.         wp_redirect( home_url( '/sorry' ), 301 );
  13.         exit;
  14.     }
  15. }
  16. add_action( 'template_redirect', 'fq_disable_single_cpt_views' );
  17.  
  18. // Order posts for featured posts first in search and categories
  19. function custom_special_sort( $query ) {
  20.  
  21.     // if is this the main query and is this post type of business
  22.     if ( $query->is_main_query() && is_post_type_archive( 'business' ) ) {
  23.  
  24.         // order results by the meta_key 'featured_listing'
  25.        // $query->set( 'meta_key', 'featured_listing' );
  26.         $query->set( 'orderby', 'featured_listing' );
  27.         $query->set( 'order', 'DESC' );
  28.     }
  29. }
  30. add_action( 'pre_get_posts', 'custom_special_sort' );
  31.  
  32.    
  33. // Make sure custom post type can be searched.     
  34. function custom_cpt_search( $query ) {
  35.        if ( is_search() && $query->is_main_query() && $query->get( 's' ) ){
  36.             $query->set('post_type', array('business'));
  37.         }
  38.         return $query; 
  39.     };
  40.    
  41. add_filter('pre_get_posts', 'custom_cpt_search');
  42.  
  43.  
  44. /**
  45.  * Extend WordPress search to include custom fields
  46.  *
  47.  * https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
  48.  */
  49.  
  50. /**
  51.  * Join posts and postmeta tables
  52.  *
  53.  * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
  54.  */
  55. function rivmedia_search_join( $join ) {
  56.     global $wpdb;
  57.  
  58.     if ( is_search() ) {    
  59.         $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
  60.     }
  61.  
  62.     return $join;
  63. }
  64. add_filter('posts_join', 'rivmedia_search_join' );
  65.  
  66. /**
  67.  * Modify the search query with posts_where
  68.  *
  69.  * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
  70.  */
  71. function rivmedia_search_where( $where ) {
  72.     global $pagenow, $wpdb;
  73.  
  74.     if ( is_search() ) {
  75.         $where = preg_replace(
  76.             "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
  77.             "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
  78.     }
  79.  
  80.     return $where;
  81. }
  82. add_filter( 'posts_where', 'rivmedia_search_where' );
  83.  
  84. /**
  85.  * Prevent duplicates
  86.  *
  87.  * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
  88.  */
  89. function rivmedia_search_distinct( $where ) {
  90.     global $wpdb;
  91.  
  92.     if ( is_search() ) {
  93.         return "DISTINCT";
  94.     }
  95.  
  96.     return $where;
  97. }
  98. add_filter( 'posts_distinct', 'rivmedia_search_distinct' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement