chenGOD Posted November 3, 2014 Report Share Posted November 3, 2014 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. Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures 백호야~~~항상에 사랑할거예요.나의 아들. Shout outs to the saracens, musulmen and celestials. Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/ Share on other sites More sharing options...
marian Posted November 3, 2014 Report Share Posted November 3, 2014 well i don't see a question Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2246261 Share on other sites More sharing options...
mcbpete Posted November 3, 2014 Report Share Posted November 3, 2014 I reckon there's a missing semi colon somewhere Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures I haven't eaten a Wagon Wheel since 07/11/07... ilovecubus.co.uk - 25ml of mp3 taken twice daily. Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2246268 Share on other sites More sharing options...
Guest skytree Posted November 4, 2014 Report Share Posted November 4, 2014 On 11/3/2014 at 6:51 PM, chenGOD said: 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? Quote Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2246326 Share on other sites More sharing options...
Glunk Posted November 4, 2014 Report Share Posted November 4, 2014 let's see ur code Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2246379 Share on other sites More sharing options...
chenGOD Posted November 4, 2014 Author Report Share Posted November 4, 2014 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" );' ) ); Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures 백호야~~~항상에 사랑할거예요.나의 아들. Shout outs to the saracens, musulmen and celestials. Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2246626 Share on other sites More sharing options...
Guest skytree Posted November 4, 2014 Report Share Posted November 4, 2014 Word. It'll take a bit to sort through but I'll try my hand at it Chen. Quote Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2246658 Share on other sites More sharing options...
mcbpete Posted November 4, 2014 Report Share Posted November 4, 2014 There's this code for an alternative API to sort by date, but I'm sure it can be bunged into that one (somewhere) (possibly) - https://swiftype.com/documentation/tutorials/dynamically_changing_sort_order_with_wordpress Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures I haven't eaten a Wagon Wheel since 07/11/07... ilovecubus.co.uk - 25ml of mp3 taken twice daily. Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2246662 Share on other sites More sharing options...
chenGOD Posted November 4, 2014 Author Report Share Posted November 4, 2014 Cheers both of you. Will have a shot at your solution tomorrow Pete. Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures 백호야~~~항상에 사랑할거예요.나의 아들. Shout outs to the saracens, musulmen and celestials. Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2246732 Share on other sites More sharing options...
Centurix Posted November 5, 2014 Report Share Posted November 5, 2014 (edited) 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. Edited November 5, 2014 by Centurix Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2246846 Share on other sites More sharing options...
chenGOD Posted November 6, 2014 Author Report Share Posted November 6, 2014 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? Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures 백호야~~~항상에 사랑할거예요.나의 아들. Shout outs to the saracens, musulmen and celestials. Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2247569 Share on other sites More sharing options...
mcbpete Posted November 6, 2014 Report Share Posted November 6, 2014 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) { Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures I haven't eaten a Wagon Wheel since 07/11/07... ilovecubus.co.uk - 25ml of mp3 taken twice daily. Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2247570 Share on other sites More sharing options...
Centurix Posted November 7, 2014 Report Share Posted November 7, 2014 cool, ta Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2247727 Share on other sites More sharing options...
chenGOD Posted November 8, 2014 Author Report Share Posted November 8, 2014 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? Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures 백호야~~~항상에 사랑할거예요.나의 아들. Shout outs to the saracens, musulmen and celestials. Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2248163 Share on other sites More sharing options...
Centurix Posted November 8, 2014 Report Share Posted November 8, 2014 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'); Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide all signatures Link to comment https://forum.watmm.com/topic/85571-any-wordpressphp-people-in-here/#findComment-2248180 Share on other sites More sharing options...
Recommended Posts