Skip to main content
replaced http://us1.php.net with https://www.php.net
Source Link

Short answer: You can't. Not like that, anyway.

You could, however, grab all of the results from your query, regardless of the presence of that custom field, and completely unsorted, and then sort them using the PHP function uksort http://us1.php.net/manual/en/function.uksort.phphttps://www.php.net/manual/en/function.uksort.php

Something like:

$args = array(
      'cat' => 5,
      'numberposts' => 8
    );
$posts = get_posts($args);
uksort($posts,"sort_posts_by_custom_order");

function sort_posts_by_custom_order($a,$b)
{
  $custom_a = get_post_meta($a->ID,'custom_sort_order',true);
  $custom_b = get_post_meta($b->ID,'custom_sort_order',true);
  $custom_a = isset($custom_a) ? $custom_a : -1;
  $custom_b = isset($custom_b) ? $custom_b : -1;
  if ($custom_a == $custom_b) { return 0; }
  return ($custom_a < $custom_b) ? -1 : 1;      
}

Definitely a lot more expensive than making one DB call, but that's the price you pay for inconsistent data.

Short answer: You can't. Not like that, anyway.

You could, however, grab all of the results from your query, regardless of the presence of that custom field, and completely unsorted, and then sort them using the PHP function uksort http://us1.php.net/manual/en/function.uksort.php

Something like:

$args = array(
      'cat' => 5,
      'numberposts' => 8
    );
$posts = get_posts($args);
uksort($posts,"sort_posts_by_custom_order");

function sort_posts_by_custom_order($a,$b)
{
  $custom_a = get_post_meta($a->ID,'custom_sort_order',true);
  $custom_b = get_post_meta($b->ID,'custom_sort_order',true);
  $custom_a = isset($custom_a) ? $custom_a : -1;
  $custom_b = isset($custom_b) ? $custom_b : -1;
  if ($custom_a == $custom_b) { return 0; }
  return ($custom_a < $custom_b) ? -1 : 1;      
}

Definitely a lot more expensive than making one DB call, but that's the price you pay for inconsistent data.

Short answer: You can't. Not like that, anyway.

You could, however, grab all of the results from your query, regardless of the presence of that custom field, and completely unsorted, and then sort them using the PHP function uksort https://www.php.net/manual/en/function.uksort.php

Something like:

$args = array(
      'cat' => 5,
      'numberposts' => 8
    );
$posts = get_posts($args);
uksort($posts,"sort_posts_by_custom_order");

function sort_posts_by_custom_order($a,$b)
{
  $custom_a = get_post_meta($a->ID,'custom_sort_order',true);
  $custom_b = get_post_meta($b->ID,'custom_sort_order',true);
  $custom_a = isset($custom_a) ? $custom_a : -1;
  $custom_b = isset($custom_b) ? $custom_b : -1;
  if ($custom_a == $custom_b) { return 0; }
  return ($custom_a < $custom_b) ? -1 : 1;      
}

Definitely a lot more expensive than making one DB call, but that's the price you pay for inconsistent data.

Source Link
heathenJesus
  • 258
  • 1
  • 2
  • 10

Short answer: You can't. Not like that, anyway.

You could, however, grab all of the results from your query, regardless of the presence of that custom field, and completely unsorted, and then sort them using the PHP function uksort http://us1.php.net/manual/en/function.uksort.php

Something like:

$args = array(
      'cat' => 5,
      'numberposts' => 8
    );
$posts = get_posts($args);
uksort($posts,"sort_posts_by_custom_order");

function sort_posts_by_custom_order($a,$b)
{
  $custom_a = get_post_meta($a->ID,'custom_sort_order',true);
  $custom_b = get_post_meta($b->ID,'custom_sort_order',true);
  $custom_a = isset($custom_a) ? $custom_a : -1;
  $custom_b = isset($custom_b) ? $custom_b : -1;
  if ($custom_a == $custom_b) { return 0; }
  return ($custom_a < $custom_b) ? -1 : 1;      
}

Definitely a lot more expensive than making one DB call, but that's the price you pay for inconsistent data.