Skip to main content
added 64 characters in body
Source Link

I created a custom where clause. I tested it using $wp_query->request right before my main loop, I don't really know SQL that well, but this seemed to get things working.

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  //Overwrite query arguments
  $query->set('meta_query', array(
    array(
      'key' => 'TRENDING',
      //'value' => 'asdfasdf',//may need a value for older versions of WordPress
      'compare' => 'NOT EXISTS',
    )
  ));
  $query->set('orderby', 'meta_value_num date');
  $query->set('order', 'DESC');
}

add_filter('posts_where', 'add_trending_where');
function add_trending_where($where = ''){
  global $wpdb, $wp_query;
  if(!$wp_query->is_main_query())//Not sure if this really works.  Should be OK
    return $where;

  $where .= " OR ( $wpdb->postmeta.meta_key = 'TRENDING' )";

  // Don't run this twice
  remove_filter('posts_where', 'add_trending_where');

  return $where;
}

Alternatively, you could set compare to 'EXISTS' and change the line in add_trending_where to $where .= " OR ($wpdb->postmeta.post_id IS NULL)";. Then you'd only have to change the value of the key in one place. Again, echo $wp_query->request and play around if you want understand this better or tweak it.

EDIT: I just noticed this does not work if meta_key is set on the query. You could use $query->set('meta_key', NULL); if you have to.

EDIT 2: I got this working with the method above. For some reason it wasn't at first (maybe meta_key was set... I don't know).

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  // Bail if not the main "hidden" query, as opposed to a 'new WP_Query()' call
  if(!$query->is_main_query())
    return;

  // Set meta_query to get shares for orderby, and also get non-shared content.
  $query->set('meta_query', array(
    'relation' => 'OR',
    array(
      'key' => 'TRENDING',
      'compare' => 'NOT EXISTS',
    ),
    array(
      'key' => 'TRENDING',
      'compare' => 'EXISTS',
    )
  ));
  //$query->set('meta_key', NULL);
  $query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC'));
}

I created a custom where clause. I tested it using $wp_query->request right before my main loop, I don't really know SQL that well, but this seemed to get things working.

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  //Overwrite query arguments
  $query->set('meta_query', array(
    array(
      'key' => 'TRENDING',
      //'value' => 'asdfasdf',//may need a value for older versions of WordPress
      'compare' => 'NOT EXISTS',
    )
  ));
  $query->set('orderby', 'meta_value_num date');
  $query->set('order', 'DESC');
}

add_filter('posts_where', 'add_trending_where');
function add_trending_where($where = ''){
  global $wpdb, $wp_query;
  if(!$wp_query->is_main_query())//Not sure if this really works.  Should be OK
    return $where;

  $where .= " OR ( $wpdb->postmeta.meta_key = 'TRENDING' )";

  // Don't run this twice
  remove_filter('posts_where', 'add_trending_where');

  return $where;
}

Alternatively, you could set compare to 'EXISTS' and change the line in add_trending_where to $where .= " OR ($wpdb->postmeta.post_id IS NULL)";. Then you'd only have to change the value of the key in one place. Again, echo $wp_query->request and play around if you want understand this better or tweak it.

EDIT: I just noticed this does not work if meta_key is set on the query. You could use $query->set('meta_key', NULL); if you have to.

EDIT 2: I got this working with the method above. For some reason it wasn't at first (maybe meta_key was set... I don't know).

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  // Set meta_query to get shares for orderby, and also get non-shared content.
  $query->set('meta_query', array(
    'relation' => 'OR',
    array(
      'key' => 'TRENDING',
      'compare' => 'NOT EXISTS',
    ),
    array(
      'key' => 'TRENDING',
      'compare' => 'EXISTS',
    )
  ));
  //$query->set('meta_key', NULL);
  $query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC'));
}

I created a custom where clause. I tested it using $wp_query->request right before my main loop, I don't really know SQL that well, but this seemed to get things working.

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  //Overwrite query arguments
  $query->set('meta_query', array(
    array(
      'key' => 'TRENDING',
      //'value' => 'asdfasdf',//may need a value for older versions of WordPress
      'compare' => 'NOT EXISTS',
    )
  ));
  $query->set('orderby', 'meta_value_num date');
  $query->set('order', 'DESC');
}

add_filter('posts_where', 'add_trending_where');
function add_trending_where($where = ''){
  global $wpdb, $wp_query;
  if(!$wp_query->is_main_query())//Not sure if this really works.  Should be OK
    return $where;

  $where .= " OR ( $wpdb->postmeta.meta_key = 'TRENDING' )";

  // Don't run this twice
  remove_filter('posts_where', 'add_trending_where');

  return $where;
}

Alternatively, you could set compare to 'EXISTS' and change the line in add_trending_where to $where .= " OR ($wpdb->postmeta.post_id IS NULL)";. Then you'd only have to change the value of the key in one place. Again, echo $wp_query->request and play around if you want understand this better or tweak it.

EDIT: I just noticed this does not work if meta_key is set on the query. You could use $query->set('meta_key', NULL); if you have to.

EDIT 2: I got this working with the method above. For some reason it wasn't at first (maybe meta_key was set... I don't know).

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  // Bail if not the main "hidden" query, as opposed to a 'new WP_Query()' call
  if(!$query->is_main_query())
    return;

  // Set meta_query to get shares for orderby, and also get non-shared content.
  $query->set('meta_query', array(
    'relation' => 'OR',
    array(
      'key' => 'TRENDING',
      'compare' => 'NOT EXISTS',
    ),
    array(
      'key' => 'TRENDING',
      'compare' => 'EXISTS',
    )
  ));
  //$query->set('meta_key', NULL);
  $query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC'));
}
added 149 characters in body
Source Link

I created a custom where clause. I tested it using $wp_query->request right before my main loop, I don't really know SQL that well, but this seemed to get things working.

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  //Overwrite query arguments
  $query->set('meta_query', array(
    array(
      'key' => 'TRENDING',
      //'value' => 'asdfasdf',//may need a value for older versions of WordPress
      'compare' => 'NOT EXISTS',
    )
  ));
  $query->set('orderby', 'meta_value_num date');
  $query->set('order', 'DESC');
}

add_filter('posts_where', 'add_trending_where');
function add_trending_where($where = ''){
  global $wpdb, $wp_query;
  if(!$wp_query->is_main_query())//Not sure if this really works.  Should be OK
    return $where;

  $where .= " OR ( $wpdb->postmeta.meta_key = 'TRENDING' )";

  // Don't run this twice
  remove_filter('posts_where', 'add_trending_where');

  return $where;
}

Alternatively, you could set compare to 'EXISTS' and change the line in add_trending_where to $where .= " OR ($wpdb->postmeta.post_id IS NULL)";. Then you'd only have to change the value of the key in one place. Again, echo $wp_query->request and play around if you want understand this better or tweak it.

EDIT: I just noticed this does not work if meta_key is set on the query. You could use $query->set('meta_key', NULL); if you have to.

EDIT 2: I got this working with the method above. For some reason it wasn't at first (maybe meta_key was set... I don't know).

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  // Set meta_query to get shares for orderby, and also get non-shared content.
  $query->set('meta_query', array(
    'relation' => 'OR',
    array(
      'key' => 'TRENDING',
      'compare' => 'NOT EXISTS',
    ),
    array(
      'key' => 'TRENDING',
      'compare' => 'EXISTS',
    )
  ));
  //$query->set('meta_key', NULL);
  $query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC'));
}

I created a custom where clause. I tested it using $wp_query->request right before my main loop, I don't really know SQL that well, but this seemed to get things working.

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  //Overwrite query arguments
  $query->set('meta_query', array(
    array(
      'key' => 'TRENDING',
      //'value' => 'asdfasdf',//may need a value for older versions of WordPress
      'compare' => 'NOT EXISTS',
    )
  ));
  $query->set('orderby', 'meta_value_num date');
  $query->set('order', 'DESC');
}

add_filter('posts_where', 'add_trending_where');
function add_trending_where($where = ''){
  global $wpdb, $wp_query;
  if(!$wp_query->is_main_query())//Not sure if this really works.  Should be OK
    return $where;

  $where .= " OR ( $wpdb->postmeta.meta_key = 'TRENDING' )";

  // Don't run this twice
  remove_filter('posts_where', 'add_trending_where');

  return $where;
}

Alternatively, you could set compare to 'EXISTS' and change the line in add_trending_where to $where .= " OR ($wpdb->postmeta.post_id IS NULL)";. Then you'd only have to change the value of the key in one place. Again, echo $wp_query->request and play around if you want understand this better or tweak it.

EDIT: I just noticed this does not work if meta_key is set on the query. You could use $query->set('meta_key', NULL); if you have to.

I created a custom where clause. I tested it using $wp_query->request right before my main loop, I don't really know SQL that well, but this seemed to get things working.

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  //Overwrite query arguments
  $query->set('meta_query', array(
    array(
      'key' => 'TRENDING',
      //'value' => 'asdfasdf',//may need a value for older versions of WordPress
      'compare' => 'NOT EXISTS',
    )
  ));
  $query->set('orderby', 'meta_value_num date');
  $query->set('order', 'DESC');
}

add_filter('posts_where', 'add_trending_where');
function add_trending_where($where = ''){
  global $wpdb, $wp_query;
  if(!$wp_query->is_main_query())//Not sure if this really works.  Should be OK
    return $where;

  $where .= " OR ( $wpdb->postmeta.meta_key = 'TRENDING' )";

  // Don't run this twice
  remove_filter('posts_where', 'add_trending_where');

  return $where;
}

Alternatively, you could set compare to 'EXISTS' and change the line in add_trending_where to $where .= " OR ($wpdb->postmeta.post_id IS NULL)";. Then you'd only have to change the value of the key in one place. Again, echo $wp_query->request and play around if you want understand this better or tweak it.

EDIT: I just noticed this does not work if meta_key is set on the query. You could use $query->set('meta_key', NULL); if you have to.

EDIT 2: I got this working with the method above. For some reason it wasn't at first (maybe meta_key was set... I don't know).

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  // Set meta_query to get shares for orderby, and also get non-shared content.
  $query->set('meta_query', array(
    'relation' => 'OR',
    array(
      'key' => 'TRENDING',
      'compare' => 'NOT EXISTS',
    ),
    array(
      'key' => 'TRENDING',
      'compare' => 'EXISTS',
    )
  ));
  //$query->set('meta_key', NULL);
  $query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC'));
}
added 149 characters in body
Source Link

I created a custom where clause. I tested it using $wp_query->request right before my main loop, I don't really know SQL that well, but this seemed to get things working.

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  //Overwrite query arguments
  $query->set('meta_query', array(
    array(
      'key' => 'TRENDING',
      //'value' => 'asdfasdf',//may need a value for older versions of WordPress
      'compare' => 'NOT EXISTS',
    )
  ));
  $query->set('orderby', 'meta_value_num date');
  $query->set('order', 'DESC');
}

add_filter('posts_where', 'add_trending_where');
function add_trending_where($where = ''){
  global $wpdb, $wp_query;
  if(!$wp_query->is_main_query())//Not sure if this really works.  Should be OK
    return $where;

  $where .= " OR ( $wpdb->postmeta.meta_key = 'TRENDING' )";

  // Don't run this twice
  remove_filter('posts_where', 'add_trending_where');

  return $where;
}

Alternatively, you could set compare to 'EXISTS' and change the line in add_trending_where to $where .= " OR ($wpdb->postmeta.post_id IS NULL)";. Then you'd only have to change the value of the key in one place. Again, echo $wp_query->request and play around if you want understand this better or tweak it.

EDIT: I just noticed this does not work if meta_key is set on the query. You could use $query->set('meta_key', NULL); if you have to.

I created a custom where clause. I tested it using $wp_query->request right before my main loop, I don't really know SQL that well, but this seemed to get things working.

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  //Overwrite query arguments
  $query->set('meta_query', array(
    array(
      'key' => 'TRENDING',
      //'value' => 'asdfasdf',//may need a value for older versions of WordPress
      'compare' => 'NOT EXISTS',
    )
  ));
  $query->set('orderby', 'meta_value_num date');
  $query->set('order', 'DESC');
}

add_filter('posts_where', 'add_trending_where');
function add_trending_where($where = ''){
  global $wpdb, $wp_query;
  if(!$wp_query->is_main_query())//Not sure if this really works.  Should be OK
    return $where;

  $where .= " OR ( $wpdb->postmeta.meta_key = 'TRENDING' )";

  // Don't run this twice
  remove_filter('posts_where', 'add_trending_where');

  return $where;
}

Alternatively, you could set compare to 'EXISTS' and change the line in add_trending_where to $where .= " OR ($wpdb->postmeta.post_id IS NULL)";. Then you'd only have to change the value of the key in one place. Again, echo $wp_query->request and play around if you want understand this better or tweak it.

I created a custom where clause. I tested it using $wp_query->request right before my main loop, I don't really know SQL that well, but this seemed to get things working.

add_action('pre_get_posts', 'add_trending_sort', 11, 1);
function add_trending_sort($query){
  if(!$query->is_main_query())
    return;

  //Overwrite query arguments
  $query->set('meta_query', array(
    array(
      'key' => 'TRENDING',
      //'value' => 'asdfasdf',//may need a value for older versions of WordPress
      'compare' => 'NOT EXISTS',
    )
  ));
  $query->set('orderby', 'meta_value_num date');
  $query->set('order', 'DESC');
}

add_filter('posts_where', 'add_trending_where');
function add_trending_where($where = ''){
  global $wpdb, $wp_query;
  if(!$wp_query->is_main_query())//Not sure if this really works.  Should be OK
    return $where;

  $where .= " OR ( $wpdb->postmeta.meta_key = 'TRENDING' )";

  // Don't run this twice
  remove_filter('posts_where', 'add_trending_where');

  return $where;
}

Alternatively, you could set compare to 'EXISTS' and change the line in add_trending_where to $where .= " OR ($wpdb->postmeta.post_id IS NULL)";. Then you'd only have to change the value of the key in one place. Again, echo $wp_query->request and play around if you want understand this better or tweak it.

EDIT: I just noticed this does not work if meta_key is set on the query. You could use $query->set('meta_key', NULL); if you have to.

added 236 characters in body
Source Link
Loading
Source Link
Loading