Created
September 15, 2011 19:29
-
-
Save tott/1220235 to your computer and use it in GitHub Desktop.
hide certain posts from search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ( function_exists( 'wpcom_is_vip' ) && wpcom_is_vip() ) { | |
wpcom_vip_load_helper_wpcom(); | |
} else { | |
require_once( WP_CONTENT_DIR . '/themes/vip/plugins/vip-do-not-include-on-wpcom/vip-local-development-helper/vip-local-development-helper.php' ); | |
require_once( WP_CONTENT_DIR . '/themes/vip/plugins/vip-do-not-include-on-wpcom/vip-powered-wpcom/vip-powered-wpcom.php' ); | |
} | |
wpcom_vip_load_helper(); | |
wpcom_vip_load_plugin( 'easy-custom-fields' ); | |
/** | |
* Setup a custom field for posts we want to hide | |
*/ | |
if ( !class_exists( "Easy_CF_Field_YesNo" ) && class_exists( "Easy_CF_Field" ) ) { | |
class Easy_CF_Field_YesNo extends Easy_CF_Field { | |
public function print_form( $object='', $box='' ) { | |
$class = ( empty( $this->_field_data['class'] ) ) ? $this->_field_data['id'] . '_class' : $this->_field_data['class']; | |
$input_class = ( empty( $this->_field_data['input_class'] ) ) ? $this->_field_data['id'] . '_input_class' : $this->_field_data['input_class']; | |
$id = ( empty( $this->_field_data['id'] ) ) ? $this->_field_data['id'] : $this->_field_data['id']; | |
$label = ( empty( $this->_field_data['label'] ) ) ? $this->_field_data['id'] : $this->_field_data['label']; | |
$value = $this->get(); | |
$hint = ( empty( $this->_field_data['hint'] ) ) ? '' : '<p><em>' . $this->_field_data['hint'] . '</em></p>'; | |
$select = $this->get(); | |
if ( !empty($select) ) | |
$selected_terms[] = $select; | |
if ( !isset( $selected_terms ) && isset( $this->_field_data['args']['default'] ) ) | |
$selected_terms[] = $this->_field_data['args']['default']; | |
$terms = array( 'yes' => 'yes', 'no' => 'no' ); | |
$values = ''; | |
foreach( $terms as $term_id => $term ) { | |
$selected = ''; | |
if ( in_array( $term_id, $selected_terms ) ) | |
$selected = 'selected="selected" '; | |
$values .= '<option ' . $selected . 'value="' . $term_id . '">' . $term . '</option>' . "\n"; | |
} | |
$label_format = | |
'<div class="%s">'. | |
'<p><label for="%s"><strong>%s</strong></label></p>'. | |
'<p><select class="%s" style="width: 100%%;" name="%s">'. | |
'%s'. | |
'</select></p>'. | |
'%s'. | |
'</div>'; | |
printf( $label_format, $class, $id, $label, $input_class, $id, $values, $hint ); | |
} | |
} | |
} | |
$field_data = array ( | |
'hide_posts' => array ( | |
'fields' => array( | |
'hide_post_search' => array( 'label' => 'Hide from search', 'hint' => 'Hide this post from search results', 'type' => 'YesNo' ), | |
), | |
'title' => 'Post visibility', | |
'context' => 'advanced', | |
'pages' => array( 'post' ), | |
), | |
); | |
$easy_cf = new Easy_CF( $field_data ); | |
function get_hidden_posts_search( $force_refresh = false ) { | |
$posts_to_hide_on_search = get_transient( 'posts_to_hide_on_search' ); | |
if ( false === $posts_to_hide_on_search || true === $force_refresh ) { | |
$posts_to_hide_on_search = array(); | |
$matches = get_posts( array( 'meta_key' => 'hide_post_search', 'meta_value' => 'yes', 'post_type' => 'post' ) ); | |
if ( !is_wp_error( $matches ) && !empty( $matches ) ) { | |
foreach( $matches as $post ) | |
$posts_to_hide_on_search[] = $post->ID; | |
} | |
set_transient( 'posts_to_hide_on_search', $posts_to_hide_on_search ); | |
} | |
return $posts_to_hide_on_search; | |
} | |
add_filter( 'parse_query', 'skip_hidden_posts_search' ); | |
function skip_hidden_posts_search( $request ) { | |
if ( true === $request->is_search ) { | |
$posts_to_hide_on_search = get_hidden_posts_search(); | |
if ( is_array( $posts_to_hide_on_search ) && !empty( $posts_to_hide_on_search ) ) | |
$request->query_vars['post__not_in'] = $posts_to_hide_on_search; | |
} | |
return $request; | |
} | |
add_action( 'save_post', 'refresh_posts_to_hide_on_search' ); | |
function refresh_posts_to_hide_on_search() { | |
get_hidden_posts_search( $force_refresh = true ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment