1

When pulling numeric data ( a number ) which has been saved to post_meta when editing a page - for example:

$number = get_post_meta( $post->ID, 'number', true ); 

And then using that value with get_posts in a meta_query - for example:

$args = array (
    'posts_per_page'    => 4, // limit ##
    'post_type'         => 'post',
    'post__not_in'      => array ( $post->ID ), // exclude self ##
    'meta_query'        => array (
        array (
            'key'       => 'related_page',
            'value'     => $number,
        )
    ),
);

$posts = get_posts( $args );

Do we first need to type juggle the retrieved value to be an integer, as it appears to be returned as a string:

var_dump($args) gives:

array(5) {
  ["posts_per_page"]=>
  int(4)
  ["post_type"]=>
  string(4) "post"
  ["post__not_in"]=>
  array(1) {
    [0]=>
    int(1387)
  }
  ["meta_query"]=>
  array(3) {
    [0]=>
    array(2) {
      ["key"]=>
      string(15) "related_page"
      ["value"]=>
      string(1) "10"
    }

  }
}

Should I be controlling the values using other custom field settings such as "compare" or "type" ?

  • @s_ha_dum - cheers for formatting that var_dump - I couldn't get it right! – Q Studio Dec 7 '13 at 16:00
  • Look for the {} button above the editor's content box. – s_ha_dum Dec 7 '13 at 16:11
2

The key and value in the postmeta table are technically always strings. even if you were to cast your string to an int it would be cast to a string by the query. Try:

$args = array (
    'post_type'         => 'post',
    'meta_query'        => array (
        array (
            'key'       => 'related_page',
            'value'     => 1,
        )
    ),
);

$posts = new WP_Query( $args );
var_dump($posts->request);

You will notice CAST(wp_postmeta.meta_value AS CHAR) = '1') in that request.

You have to use the type argument to get the query to treat the value as a number. Even with 'type' => 'NUMERIC' you will notice that the value is still treated as a string but cast by the query-- CAST(wp_postmeta.meta_value AS SIGNED) = '1').

And none of this really effects the data returned. Try:

update_post_meta(1,'hal9000',1);
var_dump(get_post_meta(1,'hal9000'));

Put in an integer, get back a string.

PHP is very forgiving about "type" so in many cases it doesn't matter whether you have a string or a int. The big case where it does matter, that I can think of, is sorting. Or, maybe, the case that shows up most on this site. You will need to cast to a integer-- use type if you need the query to sort-- or sorting will be alphabetical and not numeric which do not always align.

-1

The above answer is NOT correct. Here is what you need to do to search between min and max values:

$query_args['meta_query'][] = array(
  'key' => 'your_key_which_is_a_number',
  'value' => array(intval($minimum), intval($maximum)),
  'compare' => 'BETWEEN',
  'type' => 'NUMERIC'
 );

Simply follow this example to make up any other type of search.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.