Skip to content

Commit

Permalink
fix woocommerce issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aurovrata committed Apr 8, 2020
1 parent 3609e0a commit 92e3d88
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
4 changes: 3 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: aurovrata, aurelien
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=36PNU5KYMP738
Tags: order, reorder, re-order, order by category,order custom post type, order by categories, order category, order categories, order by taxonomy, order by taxonomies, manual order, order posts
Requires at least: 3.4
Tested up to: 5.3.0
Tested up to: 5.4.0
Requires PHP: 5.6
Stable tag: trunk
License: GPLv2
Expand Down Expand Up @@ -193,6 +193,8 @@ function custom_intial_order($ranking, $term_id, $taxonomy, $post_type){
**NOTE**: in all 3 cases, you may use the reset button (see screenshot #3) on the reorder admin page to get the filters to change the order.

== Changelog ==
= 2.4.3 =
* fix WooCommerce issues.
= 2.4.2 =
* fix sql error on old table.
= 2.4.1 =
Expand Down
24 changes: 14 additions & 10 deletions admin/class-reorder-post-within-categories-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public function load_posts(){
$reset = false;
if(isset($_POST['reset'])) $reset=$_POST['reset'];
$results = array();
// debug_msg($post_type, $term_id.' type ');
if(!empty($post_type) && $term_id>0){
/** @since 2.1.0. allow rank reset*/
if($reset) $this->_unrank_all_posts($term_id, $post_type);
Expand Down Expand Up @@ -360,7 +361,7 @@ protected function _get_order($post_type, $term_id, $start=0, $length=null){
FROM {$wpdb->postmeta} as rpwc_pm, {$wpdb->posts} as rpwc_p
WHERE rpwc_pm.meta_key ='_rpwc2'
AND rpwc_pm.meta_value=%d
AND rpwc_pm.post_id=p.ID
AND rpwc_pm.post_id=rpwc_p.ID
AND rpwc_p.post_type=%s
ORDER BY rpwc_pm.meta_id", $term_id, $post_type);

Expand All @@ -379,18 +380,20 @@ protected function _get_order($post_type, $term_id, $start=0, $length=null){
WHERE rpwc.category_id = %d AND wp.post_type=%s order by rpwc.id", $term_id, $post_type));
}
if(empty($ranking)){
$orderby = 'p.post_date';
$orderby = 'rpwc_p.post_date';
if(apply_filters('reorder_posts_within_category_initial_orderby', false, $post_type, $term_id)){
$orderby = 'p.post_name';
$orderby = 'rpwc_p.post_name';
}
$order = 'DESC';
if(apply_filters('reorder_posts_within_category_initial_order', false, $post_type, $term_id)){
$order = 'ASC';
}
$sql = $wpdb->prepare("SELECT rpwc_p.ID FROM {$wpdb->posts} as rpwc_p LEFT JOIN {$wpdb->term_relationships} AS rpwc_tr ON rpwc_p.ID=rpwc_tr.object_id
$sql = $wpdb->prepare("SELECT rpwc_p.ID FROM {$wpdb->posts} as rpwc_p
LEFT JOIN {$wpdb->term_relationships} AS rpwc_tr ON rpwc_p.ID=rpwc_tr.object_id
LEFT JOIN {$wpdb->term_taxonomy} AS rpwc_tt ON rpwc_tr.term_taxonomy_id = rpwc_tt.term_taxonomy_id
WHERE rpwc_p.post_status='publish'
AND rpwc_p.post_type=%s
AND rpwc_tr.term_taxonomy_id=%d
AND rpwc_tt.term_id=%d
ORDER BY {$orderby} {$order}", $post_type, $term_id);
/** @since 2.4.3 */
$this->filter_query($sql, "SELECT rpwc_p.ID");
Expand Down Expand Up @@ -423,11 +426,12 @@ protected function _get_order($post_type, $term_id, $start=0, $length=null){
*/
protected function count_posts_in_term($post_type, $term_id){
global $wpdb;
$sql = $wpdb->prepare("SELECT COUNT(p.ID) as total
FROM {$wpdb->posts} as p LEFT JOIN {$wpdb->term_relationships} AS tr ON p.ID=tr.object_id1
WHERE p.post_status='publish'
AND p.post_type=%s
AND tr.term_taxonomy_id=%d", $post_type, $term_id);
$sql = $wpdb->prepare("SELECT COUNT(rpwc_p.ID) as total FROM {$wpdb->posts} as rpwc_p
LEFT JOIN {$wpdb->term_relationships} AS rpwc_tr ON rpwc_p.ID=rpwc_tr.object_id
LEFT JOIN {$wpdb->term_taxonomy} AS rpwc_tt ON rpwc_tr.term_taxonomy_id = rpwc_tt.term_taxonomy_id
WHERE rpwc_p.post_status='publish'
AND rpwc_p.post_type=%s
AND rpwc_tt.term_id=%d", $post_type, $term_id);
$count = $wpdb->get_results($sql);
if(empty($count)) $count = 0;
else $count = $count[0]->total;
Expand Down
49 changes: 32 additions & 17 deletions public/class-reorder-post-within-categories-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,22 @@ public function enqueue_scripts() {
*/
public function filter_posts_join($args, $wp_query){
$queriedObj = $wp_query->get_queried_object();

if (isset($queriedObj->taxonomy) && isset($queriedObj->term_id)) {
$term_id = $queriedObj->term_id;
} else {
return $args;
}
/** @since 2.3.0 check if the post type */
$type = $wp_query->query_vars['post_type'];
/** @since 2.4.3 fix for woocommerce */
if(isset($wp_query->query_vars['wc_query']) && 'product_query'==$wp_query->query_vars['wc_query']){
$type = 'product';
}
// debug_msg($wp_query, '----TYPE: ');
if( $this->is_ranked($term_id, $type, $wp_query) ){
global $wpdb;
/** @since 2.2.1 chnage from INNER JOIN to JOIN to see if fixes front-end queries*/
$args .= "LEFT JOIN {$wpdb->postmeta} AS rankpm ON {$wpdb->posts}.ID = rankpm.post_id ";
$args .= " LEFT JOIN {$wpdb->postmeta} AS rankpm ON {$wpdb->posts}.ID = rankpm.post_id ";
}

return $args;
Expand All @@ -127,38 +131,48 @@ public function filter_posts_join($args, $wp_query){
* @since 1.0.0
*/
public function filter_posts_where($args, $wp_query){
$queriedObj = $wp_query->get_queried_object();
if (isset($queriedObj->taxonomy) && isset($queriedObj->term_id)) {
$term_id = $queriedObj->term_id;
} else {
return $args;
}
/** @since 2.3.0 check if the post type */
$type = $wp_query->query_vars['post_type'];
if( $this->is_ranked($term_id, $type, $wp_query) ){
/** @since 2.3.0 check if term id is ranked for this post type. */
// if(!empty($type) && is_string($type)) $args .= " AND rankp.post_type ='{$type}'";
$args .= " AND rankpm.meta_value={$term_id} AND rankpm.meta_key='_rpwc2' ";
}
return $args;
$queriedObj = $wp_query->get_queried_object();
if (isset($queriedObj->taxonomy) && isset($queriedObj->term_id)) {
$term_id = $queriedObj->term_id;
} else {
return $args;
}
/** @since 2.3.0 check if the post type */
$type = $wp_query->query_vars['post_type'];
/** @since 2.4.3 fix for woocommerce */
if(isset($wp_query->query_vars['wc_query']) && 'product_query'==$wp_query->query_vars['wc_query']){
$type = 'product';
}
if( $this->is_ranked($term_id, $type, $wp_query) ){
/** @since 2.3.0 check if term id is ranked for this post type. */
// if(!empty($type) && is_string($type)) $args .= " AND rankp.post_type ='{$type}'";
$args .= " AND rankpm.meta_value={$term_id} AND rankpm.meta_key='_rpwc2' ";
}
return $args;
}
/**
* filter posts_where query.
* @since 1.0.0.
*/
public function filter_posts_orderby($args, $wp_query){
$queriedObj = $wp_query->get_queried_object();
// debug_msg($args, ' order by ');

if (isset($queriedObj->taxonomy) && isset($queriedObj->term_id)) {
$term_id = $queriedObj->term_id;
} else {
return $args;
}
/** @since 2.3.0 check if the post type */
$type = $wp_query->query_vars['post_type'];

/** @since 2.4.3 fix for woocommerce */
if(isset($wp_query->query_vars['wc_query']) && 'product_query'==$wp_query->query_vars['wc_query']){
$type = 'product';
}
if( $this->is_ranked($term_id, $type, $wp_query) ){
$args = "rankpm.meta_id ASC";
}

return $args;
}
/**
Expand Down Expand Up @@ -202,4 +216,5 @@ private function is_ranked($term_id, &$type, $wp_query){
}
return $is_ranked;
}

}

0 comments on commit 92e3d88

Please sign in to comment.