WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#15340 closed defect (bug) (duplicate)

meta_value needs needs more flexibility to be either a string or an integer

Reported by: jcskyrocket Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0.1
Component: General Keywords: meta_compare, meta_value
Focuses: Cc:

Description

When using meta_compare and meta_value with query_posts, the meta_value always seems to be treated as a string.

It would be very useful, to use meta_value as an integer to perform numerical queries, filters and more complex sorts.

For more background info, see this post:

http://wordpress.stackexchange.com/questions/3737/meta-compare-seems-to-be-treating-values-as-strings-instead-of-integers-as-expect

Change History (9)

comment:1 follow-up: scribu3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

This is already fixed in trunk. See #14645

comment:2 follow-up: nacin3 years ago

Note, in 3.0-, you could have used meta_value_num. What's in trunk is just more powerful and intuitive.

comment:3 in reply to: ↑ 1 jcskyrocket3 years ago

Replying to scribu:

This is already fixed in trunk. See #14645

Sorry didn't realise. Is 'trunk' the new un-released version?

comment:4 in reply to: ↑ 2 jcskyrocket3 years ago

Replying to nacin:

Note, in 3.0-, you could have used meta_value_num. What's in trunk is just more powerful and intuitive.

What is the correct way to use meta_value_num to force meta_compare to use integers? I was only able to use meta_value_num as a value for orderby.

Here is my code:

query_posts(
	array_merge(
		$wp_query->query,
		array(
			'category__and' => $mycatids,
			'meta_key' => 'price',
			'orderby' => 'meta_value_num',
			'meta_compare' => '<=',
			'meta_value' => $mymetavalue,
			'order' => 'DESC'
		)
	)
);

comment:5 jcskyrocket3 years ago

  • Resolution duplicate deleted
  • Status changed from closed to reopened

Just wondering if you saw my questions above? Wasn't sure if re-opening was the way to get a response. This is my first ticket.

comment:6 scribu3 years ago

  • Resolution set to duplicate
  • Status changed from reopened to closed

Yes, we can see responses even if the ticket is closed.

comment:7 follow-up: scribu3 years ago

What is the correct way to use meta_value_num to force meta_compare to use integers? I was only able to use meta_value_num as a value for orderby.

Yes, I think you're limited to sorting in WP 3.0.

comment:8 in reply to: ↑ 7 ; follow-up: jcskyrocket3 years ago

Replying to scribu:

What is the correct way to use meta_value_num to force meta_compare to use integers? I was only able to use meta_value_num as a value for orderby.

Yes, I think you're limited to sorting in WP 3.0.

nacin seems to think otherwise. Have seen the same advice on a number of threads. Could not figure it out though. I agree with you, meta_value_num does appear limited to sorted in 3.0.

comment:9 in reply to: ↑ 8 nacin3 years ago

Replying to jcskyrocket:

nacin seems to think otherwise. Have seen the same advice on a number of threads. Could not figure it out though. I agree with you, meta_value_num does appear limited to sorted in 3.0.

You're right, it's just for sorting. I didn't catch that this ticket was for meta_compare.

Note: See TracTickets for help on using tickets.