Wordpress: How to compare 2 float values in meta_value column? - wordpress

I have about 100 posts in wordpress, all with a meta_key of price and a value.
How would I go about searching through all posts with a value between 23.00 and 41.00 for example?
I know that 'meta_compare ' treats 'meta_value' as a string, so I can't use this to solve my problem. Anybody have any clues? Thanks

I would try to implement the posts_where filter, and modify the query there. It gets called right after the meta comparison is added, and it allows you to change the WHERE part of your query. You need to replace all instances of your meta key to CAST(meta_key AS DECIMAL(5,2)). I guess the meta key is prefixed by the meta table name, so check for that. You will need to figure out a way to pass two meta_value parameters if you want to do a BETWEEN query, but you can combine them with a comma and split them up in your filter. As long as a valid SQL where-clause comes out of you filter, everything should work.

Related

WP Query - sortby with comma in number

I'm trying to sort a query of product custom post type, ordering by the product weight (ASC - from lightest to heaviest). The product weight is entered in a Advanced Custom Fields "field" on the product edit screen.
The query is working fine, except for when the number contains a comma (ex 1,000). It will put a product with a weight of 1,000 at the top of the list (first). So is somehow not looking at the number past the comma, so views it as 1, not 1,000.
Fortunately, I was able to convince my client to leave out the commas and are using the php number_format() to display the comma on the front end.
However, I don't like relying on my client to remember to exclude the comma for the sorting to work correctly.
Is there a way to have WP Query properly sort number values with commas in them? Maybe with an action or filter to remove the comma before saving in the DB and then adding it back once it's displayed in a template file? I really don't have much experience with actions and filters in WordPress yet.
Thanks in advance.

WP Advanced custom field value in query posts array

I need to put an advanced custom field value (category number) from page to query_posts array. Is it somehow possible?
On a single page I'm trying to show posts from category, which number is value of this custom field.
Ideal and of course non-functional case:
query_posts(array('category__and'=>array( the_field("category"); ,99), 'posts_per_page'=>6));
the_field() displays a meta value. You'll want to use get_field() (which returns a meta value) instead:
query_posts(array('category__and'=>array( get_field("category"); ,99), 'posts_per_page'=>6));
In somewhat unrelated news, you should really consider avoiding the use of query_posts.

Wordpress order categories

I have a Wordpress site where a lot of the categories are peoples names. The slug becomes john-smith fred-wilkinson etc by default. So the category list is in alphabetical order by their first name. I would like them to list by last name.
I could get the authors/editors to change the slug to wilkinson-fred etc but I would rather have a solution that they don't have to alter the default slug.
I know there are plug-ins that you can drag and drop to any order you want. Again this relies on the author/editor putting it in the right place.
My slugs currently look like this
john-smith
fred-wilkinson
Alan-t-jones
etc
I have other categories that I don't want to alter. I only want to reorder where the parent category is "artist".
Any ideas? If I put wilkinson-fred in the description field can I sort on that?
Thanks
I assume that you want to change the order of wp list categories function output (maybe make it a little clearer in your future SO questions!). If that's so, I think you have two options here:
1) Hijack the orderby parameter as mentioned in this answer by #Chris_O at Wordpress Stackexchange:
There is an unused column, term_order, in the wp_term_relationships table that you can use to assign a custom order to the terms within your taxonomy. The order is set at 0 by default and it will take a custom query to get the order back and another solution to set the order.
Although as #Kaiser mentions in the comments to the answer, this sollution is a little hacky as it's not meant to be used this way and its implemantation may change in future releases of Wordpress.
2) Store the output of the function in varible (echo => false under $args), manipulate the output and print the resulting array. This is the way I would choose as it makes you more in control of the situation.

Apachesolr query - filter by text in fields

I'm constructing an apachesolr query in my Drupal module programmatically and have had success with some aspects, but am still struggling with others.
So far, I have been able to construct a query that can search for specific text and limit the results based on terms to be filtered by with the following code:
$subquery_region->addFilter('tid', $term->tid);
$query->addFilterSubQuery($subquery_region, 'OR', 'AND');
What I'd like to achieve next is to be able to narrow the search further by adding a filter for finding certain text within a specific field in the node. Has anyone been able to do this.
I've been researching online and have tried lots of different ways such as adding the filter directly to the main search query
$query->addParam('fl', 'ss_my_field');
$query->addFilter("ss_my_field", "field_substring_to_search_for");
As well as breaking that out into a subquery to add to the main search query
$subquery_test = apachesolr_drupal_query("Test");
$subquery_test->addParam('fl', 'ss_my_field');
$subquery_test->addFilter("ss_my_field", "field_substring_to_search_for");
$query->addFilterSubQuery($subquery_test, 'OR', 'AND');
But none of these are working. They are returning an empty set, even though I know the substring exists in the field I'm adding as a filter and it has been indexed. I have verified through the apachesorl views module that the search index has been populated with that field and can see the substring exists.
Is there anything wrong with my syntax or the way I'm building the query?
If you know how to add filters for searching for text within certain fields, please share! It may not even be done with the addFilter function, but that's all I have been trying so far.
Thanks!
First you have to create an index for that specific field.
function hook_apachesolr_update_index(&$document, $node) {
$document->ss_your_field_name = $node->your_field_name;
}
where ss_* is the pattern.
ss_* -> String
is_* -> Integer
im_* -> Integer, Multivalued
After that you have to
1. delete the index - admin/settings/apachesolr/index
2. re-index the content
3. run the cron
4. check the filter that you created - admin/reports/apachesolr/index
Then, you can add filters
$query->addFilter("ss_your_field_name", "value");
Hope this helps you.
function hook_apachesolr_modify_query(&$query, &$params, $caller){
$subquery = apachesolr_drupal_query();
$subquery->add_filter("ss_my_field", "field_substring_to_search_for");
$query->add_subquery($subquery, "AND");
}

Asp.Net Sql Auto-Increment for Wall Post

I have a table that contains three columns.
"UserId" type-nvarchar
"PostAuthorId" type-nvarchar
"Post" type-text
This table will contain "wall" posts like in facebook for each user's page. I am going to use a gridview on each user's page to display the posts. The issue is I want to display them with the latest(most current) post being first and the earliest post being last.
I have never used autoincrement before and I am not sure if that is the answer. If it is, I do not know how to use it. I thought about adding a date posted column and then ordering by date.
If I end up using the date column, I could also display the date on the post. Is there a way to convert the date to a readable format?
What is the best way of implementing this type of ordering?
If you use AutoIcrement the first record will start with 1 and each record will increment from there. (default setting)
If you want to sort them by newest first do an ORDER BY ID DESC
I would suggest making a column called wallPostID then setting that to AutoIncrement and also your Primary Key
Date Formating:
If you are displaying this data in a gridView
Go to Edit Columns on your grid view
CLick on the Date field under "Selected Fields" on the bottom left
Under "BoundField properties" on the right Go to Data -> DataFormatString
{0:d} will display as 1/1/2010
This site has more info in string formatting
http://msdn.microsoft.com/en-us/library/fht0f5be.aspx
A datetime column would definitely work for something like this. Assuming you are using MS-SQL, you can also attach a default value to the column using a built-in function like GETDATE(). That way, you only have to input the data that matters and the database will take care of adding the datetime column.
For converting a datetime to a readable format try:
DateTime postDate;
string value = postDate.ToShortDateString();
You should always use an ID field that auto increments. Can also be used as your PK
I would suggest the DateTime field rather than the autoincrement simply because it will not only serve as an effective Sort field, it also preserves information that you may well want to display. If you want the most recent first you'll sort using the Date and a "DESC" modifier:
Select ... Order By [Date] DESC;
When you retrieve the data, you can retrieve it as a DateTime and modify it using C#. You can use "ToShortDateString()" as suggested by mdresser if you just wish to show the date or ToString("...") if you wish to show the time as well. You can also use SQL to convert it into a string before retrieving it:
convert(Varchar(10), #mydatetime, 101)
If you look in MSDN you'll see the various conversion codes (101 is the code used above) that can be used to translate the date in various ways.
UPDATE: You may want to use an autoincrementing field for your application for reasons other than your expressed need to sort wall entries. They are easy to use - just mark the field as an Identity if using SQL Server (other DBs are similar). As far as using them in your program, just think of the field as an Int field that you never have to set.
Now, why would you use a auto-incrementing field? Perhaps the most straightforward reason is so that they give you have an easy way to identify each record. For example, if you permit people to alter or delete their wall entries, the auto-incrementing field is ideal as it gives you a way to easily look up each record (each record will be assigned its own, unique value). You might put an "x" next to the record like StackOverflow does and make it a call back with the UID (auto-increment) value. Note that you should set up your primary key on the UID field if you'll be doing this.
Now, if you find them useful for this reason then you could also sort by the UID. I would still store the date so that you can provide Date and Time feedback as to when an entry was made on the wall but this would no longer be your indexed or sorted field.

Resources