Jump to content
IGNORED

Any wordpress/PHP people in here?


chenGOD

Recommended Posts

Guest skytree

chen is a retard and is trying to hack his way through editing a widget in wordpress.

If anyone can help, you will get much karmic merit.

Hmm perhaps - what seems to be the problem sir?

Link to comment
Share on other sites

The problem is, I need the advanced search widget in the sidebar of this site: asiapacificmemo.ca to return results by most recent date.

 

Code is here:

<?php
/*
Plugin Name: Advanced Search Widget
Plugin URI: http://wordpress.org/extend/plugins/advanced-search-widget
Description: Allows you to add a widget to search custom post types along with other options
Author: Aaron Axelsen
Version: 0.3
Author URI: http://aaron.axelsen.us
Text Domain: advanced-search-widget
*/

function advancedSearchWidget_getvars($getwidget) {
	if (empty($getwidget)) return false;
	$widget = get_option('widget_advanced-search-widget');
	$instance = esc_attr($_GET['widget']);
	$id = substr($instance,strrpos($instance,'-')+1);
	$options = $widget[$id];

	$opts = array();
	$opts['searchtitle'] = (isset($options['searchtitle']) ? $options['searchtitle'] : '1');
	$opts['searchcontent'] = (isset($options['searchcontent']) ? $options['searchcontent'] : '1');
	$opts['searchtags'] = (isset($options['searchtags']) ? $options['searchtags'] : '0');
	return $opts;
}

function advancedSearchWidget_searchquery($search) {
	if (!isset($_GET['posttype'])) return $search;    

	if (is_search()) {
		if (isset($_GET['widget'])) {
			extract(advancedSearchWidget_getvars($_GET['widget']));
		}
		global $wpdb, $wp_query;

		if ( empty( $search ) )
	        	return $search; // skip processing - no search term in query

		$q = $wp_query->query_vars;    
		$n = ! empty( $q['exact'] ) ? '' : '%';

		$search = "$wpdb->posts.post_type = '".esc_attr($_GET['posttype'])."' AND ";
		$searchand = '';

		foreach( (array) $q['search_terms'] as $term ) {
			$term = esc_sql( like_escape( $term ) );

			//push search "OR's"
			$list = array();
			if (isset($searchtitle) && $searchtitle == 1)
				array_push($list,"($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')");

			if (isset($searchcontent) && $searchcontent == 1)
				array_push($list,"($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')");

			if (isset($searchtags) && $searchtags == 1)
				array_push($list,"(t.name like '{$n}{$term}{$n}' AND post_status = 'publish' and tt.taxonomy in ('post_tag', 'category'))");
	
			$search .= "{$searchand}";
			$search .= "( ";
			$search .= implode(" OR ",$list);
			$search .= ")";
			$searchand = ' AND ';
		}

		if ( ! empty( $search ) ) {
			$search = " AND ({$search}) ";
			if ( ! is_user_logged_in() )
				$search .= " AND ($wpdb->posts.post_password = '') ";
		}
	}
	return $search;
}
add_filter('posts_where','advancedSearchWidget_searchquery');

function advancedSearchWidget_searchjoin($join) {
	if (is_search()) {
                if (isset($_GET['widget'])) {
                        extract(advancedSearchWidget_getvars($_GET['widget']));

			if (isset($searchtags) && $searchtags == 1) {
				global $table_prefix, $wpdb;
				$tabletags = $table_prefix . "terms";
				$tablepost2tag = $table_prefix . "term_relationships";
				$tabletaxonomy = $table_prefix . "term_taxonomy";

				$join .= " LEFT JOIN $tablepost2tag tr ON $wpdb->posts.ID = tr.object_id INNER JOIN $tabletaxonomy tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN $tabletags t ON t.term_id = tt.term_id ";
			}
		}
	}
	return $join;
}
add_filter('posts_join','advancedSearchWidget_searchjoin');

function advancedSearchWidget_searchgroupby($groupby) {
        if (is_search()) {
                if (isset($_GET['widget'])) {
                        extract(advancedSearchWidget_getvars($_GET['widget']));

                        if (isset($searchtags) && $searchtags == 1) {
				global $wpdb;

				// we need to group on post ID
				$mygroupby = "{$wpdb->posts}.ID";
	
				if( preg_match( "/$mygroupby/", $groupby )) {
					// grouping we need is already there
					return $groupby;
	  			}
	
				if( !strlen(trim($groupby))) {
					// groupby was empty, use ours
					return $mygroupby;
				}
	
				// wasn't empty, append ours
				return $groupby . ", " . $mygroupby;
			}
		}
	}	
	return $groupby;	
}
add_filter('posts_groupby', 'advancedSearchWidget_searchgroupby');

function customsearchwidget_getquery($id) {
	$widget = esc_attr($_GET['widget']);
	$posttype = esc_attr($_GET['posttype']);
	if (!empty($widget) && !empty($posttype)) {
		if ($widget == $id) return $_GET['s'];
	} 
}

#function postsrequeststmp($sql) {
#	if (is_search()) {
#	print_r($sql);
#	}
#	return $sql;
#}
#add_filter('posts_request','postsrequeststmp');

/**
 * Adds Advanced_Search widget.
 */
class Advanced_Search_Widget extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	public function __construct() {
		parent::__construct(
	 		'advanced-search-widget', // Base ID
			'Advanced Search Widget', // Name
			array( 'description' => __( 'Advanced search widget', 'advanced-search-widget' ), ) // Args
		);
	}

	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {
		extract( $args );
		$title = apply_filters( 'widget_title', $instance['title'] );

		echo $before_widget;
		if ( ! empty( $title ) )
			echo $before_title . $title . $after_title;
	        $form = '<form role="search" method="get" id="'.$widget_id.'-searchform" action="' . esc_url( home_url( '/' ) ) . '" >
        	<div class="widget_search"><label class="screen-reader-text" for="'.$widget_id.'-s">' . __('Search for:') . '</label>
	        <input type="text" value="" name="s" id="'.$widget_id.'-s" />
		<input type="hidden" name="posttype" value="' .$instance['posttype']. '" />
		<input type="hidden" name="widget" value="' .$widget_id. '" />
        	<input type="submit" id="'.$widget_id.'-searchsubmit" value="'. esc_attr__('Search') .'" />
	        </div>
        	</form>';
		echo $form;
		echo $after_widget;
	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */
	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['posttype'] = strip_tags( $new_instance['posttype'] );
		$instance['searchtitle'] = (empty($new_instance['searchtitle']) ? '0' : strip_tags( $new_instance['searchtitle'] ));
		$instance['searchcontent'] = (empty($new_instance['searchcontent']) ? '0' : strip_tags( $new_instance['searchcontent'] ));
		$instance['searchtags'] = (empty($new_instance['searchtags']) ? '0' : strip_tags( $new_instance['searchtags'] ));
		return $instance;
	}

	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {
		if ( isset( $instance[ 'title' ] ) ) {
			$title = $instance[ 'title' ];
		}
		else {
			$title = __( 'New title', 'advanced-search-widget' );
		}
		if (isset($instance['searchtitle'])) {
			$searchtitle = ($instance['searchtitle'] == 1 ? ' checked="checked"' : '');
		} else {
			$searchtitle = ' checked="checked"';
		}
		if (isset($instance['searchcontent'])) {
			$searchcontent = ($instance['searchcontent'] == 1 ? ' checked="checked"' : '');
		} else {
			$searchcontent = ' checked="checked"';
		}
		if (isset($instance['searchtags'])) {
			$searchtags = ($instance['searchtags'] == 1 ? ' checked="checked"' : '');
		} else {
			$searchtags = '';
		}

		$custom_post_types = get_post_types( array('exclude_from_search' => false) );	
		#array_unshift($custom_post_types,'any');
		?>
		<p>
		<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		</p>
		<p><label for="<?php echo $this->get_field_id( 'posttype' ); ?>"><?php _e( 'Post Type:' ); ?></label>
		<select class='widefat' id="<?php echo $this->get_field_id( 'posttype' ); ?>" name="<?php echo $this->get_field_name( 'posttype' ); ?>">";
        		<?php foreach ($custom_post_types as $t) {
				$selected = '';
				if (isset($instance['posttype']) && $instance['posttype'] == $t) $selected = ' selected="selected"';
				echo "<option{$selected}>$t</option>";
			}?>
		</select>
		</p>
		<p><?php _e('Search using the following fields:','advanced-search-widget'); ?></p>
		<p>
		<input id="<?php echo $this->get_field_id( 'searchtitle' ); ?>" name="<?php echo $this->get_field_name( 'searchtitle' ); ?>"<?php echo $searchtitle; ?> type="checkbox" value="1" />
		<label for="<?php echo $this->get_field_id( 'searchtitle' ); ?>"><?php _e( 'Title' ); ?></label> 
		</p>
		<p>
		<input id="<?php echo $this->get_field_id( 'searchcontent' ); ?>" name="<?php echo $this->get_field_name( 'searchcontent' ); ?>"<?php echo $searchcontent; ?> type="checkbox" value="1" />
		<label for="<?php echo $this->get_field_id( 'searchcontent' ); ?>"><?php _e( 'Content' ); ?></label> 
		</p>
		<p>
		<input id="<?php echo $this->get_field_id( 'searchtags' ); ?>" name="<?php echo $this->get_field_name( 'searchtags' ); ?>"<?php echo $searchtags; ?> type="checkbox" value="1" />
		<label for="<?php echo $this->get_field_id( 'searchtags' ); ?>"><?php _e( 'Tags' ); ?></label> 
		</p>
		<?php 
	}

} // class Foo_Widget

// register advanced search widget
add_action( 'widgets_init', create_function( '', 'register_widget( "advanced_search_widget" );' ) );

Link to comment
Share on other sites

Wordpress has a filter called posts_orderby which you return your desired order. Registered in the same way as the posts_where. Add the following near the top of your code somewhere:

function advancedSearchWidget_searchorder($order, $query) {
	global $wpdb;
	if(!is_admin() && is_search()) // Add the filter for the most recent date first of each post
		$order = $wpdb->prefix."posts.post_date DESC";

	return $order;
}
add_filter('posts_orderby','advancedSearchWidget_searchorder');

Bear in mind I haven't tested this, the function name follows the widgets naming conventions and fairly it's straight forward. Should mention that the important part is the posts,post_date DESC, which is the actual ordering itself.

Link to comment
Share on other sites

Gave your code a shot Cen, returned this error:

 

Warning: Missing argument 2 for advancedSearchWidget_searchorder() in /home/content/32/6336232/html/wp-content/plugins/advanced-search-widget/advanced-search-widget.php on line 95

 

Any ideas?

Link to comment
Share on other sites

Looks like the function has two inputs ( $order and $query ) but is only using the $order bit. Complete guess but maybe change the first line to:

function advancedSearchWidget_searchorder($order) {
Link to comment
Share on other sites

Well it doesn't return an error anymore, so that's sorted.

 

I must be putting it in the wrong place in the code, cause now the first group of queries to be returned comes from the list of contributors. Then I get posts returned but also still not by any ordered date, just seems random.

 

This is the code as I have it now

function advancedSearchWidget_searchorder($order) {
	global $wpdb;
	if(!is_admin() && is_search()) // Add the filter for the most recent date first of each post
		$order = $wpdb->prefix."posts.post_date DESC";

	return $order;
}
add_filter('posts_orderby','advancedSearchWidget_searchorder');

So I guess it's just a matter of plugging in in the right place? I've set the plugin to search only tags, because the two other options are Title (not broad enough) and Content (too broad). I wish there was a way for it to search categories, but the author doesn't seem to have thought of that, and it seems like he's not actively developing it anymore.

Another alternative would of course be a different search widget...can anyone recommend one?

Link to comment
Share on other sites

Weird, the position doesn't matter as it's just a register of filters for Wordpress to process. Changed the prefixing to match the rest of the widget. Don't think that's going to fix the ordering.

function advancedSearchWidget_searchorder($order) {
	global $table_prefix;
	if(!is_admin() && is_search()) // Add the filter for the most recent date first of each post
		$order = $table_prefix."posts.post_date DESC";

	return $order;
}
add_filter('posts_orderby','advancedSearchWidget_searchorder');

Maybe replace post_date with post_modified, might be more accurate:

function advancedSearchWidget_searchorder($order) {
	global $table_prefix;
	if(!is_admin() && is_search()) // Add the filter for the most recent date first of each post
		$order = $table_prefix."posts.post_modified DESC";

	return $order;
}
add_filter('posts_orderby','advancedSearchWidget_searchorder');
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.