I have custom Field named "motor_brand" of posts.This meta values saved as ["honda","suzuki","kawasaki","toyota"]
If i search just honda by meta_query then all pos having honda meta value if iwant to search honda and kawasaki then the having honda or kawasaki meta values appear if i want to search kawasaki,honda and toyota then posts that having meta value honda, kawasaki or toyota can be appers.
I have set meta query like this
meta_ query = array(
'relation'=>'OR'
array(
meta_key =>'motor_brand'
value => "honda",
compare => "LIKE"
),
array(
meta_key =>'motor_brand'
value => "kawasaki",
compare => "LIKE"
),
)
That works fine but when i try
meta_ query = array(
'relation'=>'OR'
array(
meta_key =>'motor_brand'
value => "honda",
compare => "LIKE"
),
array(
meta_key =>'motor_brand'
value => "kawasaki",
compare => "LIKE"
),
array(
meta_key =>'motor_brand'
value => "toyota",
compare => "LIKE"
),
)
That not work
i want to search to many motor brands
Maybe you want to try this:
$meta_query = array(
array(
'key' => 'motor_brand',
'value' => array('honda','kawasaki','toyota'),
'compare' => 'IN',
)
);
It should produce WHERE motor_brand IN ('honda','kawasaki','toyota') and should show you results if you execute on your db directly.
Edit
Oh sorry, i saw you wrote
I have custom Field named "motor_name" of posts.This meta values saved
as ["honda","suzuki","kawasaki","toyota"]
The IN doesnt work if you stored it like this. But what about motor_name. Your Field is named motor_name but your query asks for motor_brand.
Related
I using advanced-custom-fields-pro in wordpress to allow wordpress user to type page id they wish to display in web, is there any way to get the data from wordpress user and in backend using wp_query and display the page with the data user entered in advanced-custom-fields-pro?
Please take note, i named the field as “feature” in plugin. For my case, ‘feature’ can be contain multiple value like 1,2,3,4 so i would like to get my ‘feature’ value in array, what can i do for this?
Currently, get_field didnt get value sucessfully.
$feature = get_field(‘feature’);
$the_query_featured = new WP_Query( array(
‘posts_per_page’ => 2,
‘posts_per_page’ => 2,
‘nopaging’ => false,
‘order’ => ‘DESC’, //’order’ => ‘ASC’,
‘orderby’ => ‘date’,
‘meta-key’ => $feature,
‘page_id’ => $feature,
‘post_type’ => ‘any’
));
<?php while ( $the_query_featured->have_posts() ) : $the_query_featured->the_post(); ?>
I want to update two tables in a database after a button is clicked.
Already using an update query separated but sometimes the second query will not run and not updating the table.
This is my update queries
$wpdb->replace($table_name_users,$data_array_users);
$wpdb->update($table_name,$data_array,$data_where);
In this case I post just integers like 0,1,2 etc to database tables this queries is related to wordpress update queries.
What is the problem and how can I fix it out ?
Should add $format and $where_format as explained in wordpress developers documentation about wpdb::update for each array in update query.
Example :
wpdb::update( ‘table’, array( ‘column’ => ‘foo’, ‘field’ => ‘bar’ ), array( ‘ID’ => 1 ) ) wpdb::update( ‘table’, array( ‘column’ => ‘foo’, ‘field’ => 1337 ), array( ‘ID’ => 1 ), array( ‘%s’, ‘%d’ ), array( ‘%d’ ) )
You can find more information here :
https://developer.wordpress.org/reference/classes/wpdb/update/
I have a custom field named type, which is a a "radio button" data type and it has some choices. This custom field, is assigned to a custom post type named pproduct.
For example here are the choices of this custom field :
RED
BLUE
YELLOW
WHITE
BLACK
Only one can be selected from the above.
The below $args :
$args = array(
'post_type' => 'pproduct',
'posts_per_page' => -1,
'post_status'=>array('publish'),
'product' => $category->slug ,
'meta_query' => array(
'relation' => 'AND',
'type_clause' => array(
'key' => 'type',
),
'order_clause' => array(
'key' => 'order',
),
),
'orderby' => array(
'type_clause' => 'DESC',
'order_clause' => 'ASC',
),
);
will query all posts of post type pproduct, and it will sort it by two custom fields. Type and order . It will sort it in an alphabetical order.
Is it possible to modify this and sort it by the same order as the types are assigned? Does anyone know what happens if i don't use order by? I can see it brings the posts but what is the "default order" if it's not assigned by me.
EDIT 1 : Something like this
UPDATE:
I missunderstood your demand. If i get it right now, you want to make order as you assigned it in setting, simply like you write
RED
BLUE
YELLOW
WHITE
BLACK
That couldn't be achieved with WP query args, you will have to write your own database query to achieve this because query must know the order rules which is set by you (it does know alpabetical, numeric, date order etc. which can be simply derivated from field).
However if you could change values ACF to numeric (like u've posted in comment link) you win, then you will have to create translation array (to translate number to color name) if u will need to use value as color name/slug.
So in ACF settings choices:
1 : RED
2 : BLUE
3 : YELLOW
4 : WHITE
5 : BLACK
Query args will remain same (except type ordering DESC->ASC) and if you need to get the name from number, use this in loop:
$field = get_field_object( 'type' );
$value = $field['value'];
$color_name = $field['choices'][ $value ]; // this will be formated
$unformated_slug = sanitize_title( $color_name ); // change to lowercase and remove whitespaces etc..
// then you can work with $unformated_slug like your original field value eg:
if( $unformated_slug == 'red' ) {
/* do something here */
}
Changing choice values to numbers is the most simplier way, anything other will be too complicated.
--
Default order of posts is by date. If you want to set your order automatically for all queries or only for custom post type queries, see https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
I'm using mongodb 2.4 and added fulltext index to the "title" field in one the collection. How should I search something in that field using php?
This is the code I use right now:
$params = array(
'_id' => array(
'$gt' => (int)$gt
)
);
$r = $this->collection->find( $params )->limit($limit);
This seem to be the answer to my question:
<?php
$result = $db->command(
array(
'text' => 'bar', //this is the name of the collection where we are searching
'search' => 'hotel', //the string to search
'limit' => 5, //the number of results, by default is 1000
'project' => Array( //the fields to retrieve from db
'title' => 1
)
)
);
http://www.php.net/manual/en/mongodb.command.php#111891
So I have been working with SugarCRM pretty extensively lately and I need to be able to display the last date a contact was emailed as well search based on how long ago a contact was emailed.
I can easily create a logic hook to get the last email date but I cannot search based on the value of that.
So I looked in SearchFields.php and found the following code which obviously gets the email of the contact.
'email' =>
array (
'query_type' => 'default',
'operator' => 'subquery',
'subquery' => 'SELECT eabr.bean_id FROM email_addr_bean_rel eabr JOIN email_addresses ea ON (ea.id = eabr.email_address_id) WHERE eabr.deleted=0 AND ea.email_address LIKE',
'db_field' =>
array (
0 => 'id',
),
),
Using that data I decided to create a new array with the following code to get the last email date. Obviously it will need to be sorted or something to get the actual last email date if the contact was emailed multiple times but you get the point.
'last_email_c' =>
array (
'query_type' => 'default',
'operator' => 'subquery',
'subquery' => 'SELECT e.date_sent FROM emails e WHERE e.parent_type = \'Contacts\' AND e.parent_id = \'{0}\'',
'db_field' =>
array (
0 => 'id',
),
),
My issue is, I don't know what to do with this new subquery, I checked in the listviewdefs and the searchdefs and cannot find any reference to either of these custom SearchFields.
What am I missing?
Try this one. Basically when you use subquery you have to assume that the parameter from the form will be added to the end of the subquery so if you search for blah#blah.com sugar will append to your subquery 'blah#blah.com%' so LIKE operator is the only way to do this
t_email_c' => array (
'query_type' => 'default',
'operator' => 'subquery',
'subquery' => 'SELECT e.date_sent FROM emails e WHERE e.parent_type = \'Contacts\' AND e.parent_id LIKE ',
'db_field' =>
array (
0 => 'id',
),
Also you should be selecting the id of the parent record so that it could then be matched against the parent record.
Basically in your subquery you should be selecting a contact ID where the last email date is like.
If you can't write query this way you will need to do it by overwriting list.view actions.