Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Remove single listings full page for basic users.
- function fq_disable_single_cpt_views() {
- $queried_post_type = get_query_var('post_type');
- $cpts_without_single_views = array( 'business' );
- $featured_listing = get_post_meta(get_the_ID(), "featured_listing", TRUE);
- if ( is_single()
- && in_array( $queried_post_type, $cpts_without_single_views )
- && ("" === $featured_listing)
- ) {
- wp_redirect( home_url( '/sorry' ), 301 );
- exit;
- }
- }
- add_action( 'template_redirect', 'fq_disable_single_cpt_views' );
- // Order posts for featured posts first in search and categories
- function custom_special_sort( $query ) {
- // if is this the main query and is this post type of business
- if ( $query->is_main_query() && is_post_type_archive( 'business' ) ) {
- // order results by the meta_key 'featured_listing'
- // $query->set( 'meta_key', 'featured_listing' );
- $query->set( 'orderby', 'featured_listing' );
- $query->set( 'order', 'DESC' );
- }
- }
- add_action( 'pre_get_posts', 'custom_special_sort' );
- // Make sure custom post type can be searched.
- function custom_cpt_search( $query ) {
- if ( is_search() && $query->is_main_query() && $query->get( 's' ) ){
- $query->set('post_type', array('business'));
- }
- return $query;
- };
- add_filter('pre_get_posts', 'custom_cpt_search');
- /**
- * Extend WordPress search to include custom fields
- *
- * https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
- */
- /**
- * Join posts and postmeta tables
- *
- * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
- */
- function rivmedia_search_join( $join ) {
- global $wpdb;
- if ( is_search() ) {
- $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
- }
- return $join;
- }
- add_filter('posts_join', 'rivmedia_search_join' );
- /**
- * Modify the search query with posts_where
- *
- * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
- */
- function rivmedia_search_where( $where ) {
- global $pagenow, $wpdb;
- if ( is_search() ) {
- $where = preg_replace(
- "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
- "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
- }
- return $where;
- }
- add_filter( 'posts_where', 'rivmedia_search_where' );
- /**
- * Prevent duplicates
- *
- * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
- */
- function rivmedia_search_distinct( $where ) {
- global $wpdb;
- if ( is_search() ) {
- return "DISTINCT";
- }
- return $where;
- }
- add_filter( 'posts_distinct', 'rivmedia_search_distinct' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement