I have a string submitted from a form field to my query, for example:
1159
The meta value is CFN-1159 but I want to be able to run the query without always using the full meta value.
My array is as follows:
//The submitted reference code
$ref = $_POST['ref'];
//The array added to the meta query
$ref_query = array(
'meta_key' => 'reference_code',
'meta_value' => array($ref),
'meta_compare' => 'LIKE'
);
Im looking to only return the post with the unique ref code (meta_value).
How could I use REGEX instead of LIKE here to find posts where the meta value "ends with" the submitted string.
Simply use something like that
$query_adresses = array (
'order' => 'ASC',
'post_type'=> 'your_post_type',
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => 'reference_code',
'value' => array($_POST['ref']),
'compare' => 'REGEXP'
)
)
);
Related
I have some programs that go according to the day but I want it to be filtered by custom field such as menu_order, that is, by numerical position through ordernum that I can give it
This doesn't show me the result. ordernum is a select field with integers
$args = array(
'post_type' => 'schedule',
'posts_per_page' => -1,
'meta_key' => 'day',
'meta_value' => 'Sunday',
'meta_query' => array(
array(
'key' => 'ordernum',
'value' => 'meta_value_num',
'compare' => '=',
),
)
);
Your meta query at the moment is filtering the results by the custom field "ordernum" with the value "meta_value_num". It seems like your value is wrong. If you want to filter the results for only posts which have ordernum = 1, then change the value to "1".
If you want to order your query by the ordernum field, you will need to add the following args
$args = array(
'orderby' => 'ordernum',
'order' => 'DESC',
);
I'm trying to get sorted list of posts using get_posts by meta value and the order of meta value is given in array.
This is what I currently have.
$stores = get_posts(array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids', // I only need the ID's of posts
'orderby' => 'meta_value',
'meta_key' => 'state',
'order' => 'ASC'
));
This returns the array of posts sorted by meta_value in ASCENDING alphabetical order.
I have an array of possible values for 'meta_key' => 'state', i.e. array('State1', 'State2', 'State3')
I want to set order so that all stores which has meta value State1 appears first, then from State2 and after that State3
I can't use order by numeric value and alphabetical value as state names are gonna be random.
I found one post here, it is using mera_query_orderby. I can't find any documentation for this and tried it, but it's not working. It returns posts ordered by ID.
Any help would be appreciated. Thanks
EDIT:
I added the meta_query_orderby filters in functions.php
And the updated code I used from EXAMPLE 2, is like:
$stores = get_posts(array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'state', // Custom field key.
'value' => array("CState1", "AState2", "BState3")
)
),
'meta_query_orderby' => array(
array(
'key' => 'state', // (required) Custom field key.
'value' => array("CState1", "AState2", "BState3")
)
)
));
I have an array of possible values for 'meta_key' => 'state', i.e.
array('State1', 'State2', 'State3')
If you want to sort the posts by the meta value in the exact order as in the above array, you can use a custom WP_Query parameter (to set the meta/sort values) and the posts_orderby filter to customize the ORDER BY clause, and in that clause, you would be using the FIELD() function in MySQL.
Step 1
Add this code to your plugin or theme (if theme, you'd add the code to the theme functions file):
add_filter( 'posts_orderby', 'posts_orderby_meta_value_list', 10, 2 );
function posts_orderby_meta_value_list( $orderby, $query ) {
$key = 'meta_value_list';
if ( $key === $query->get( 'orderby' ) &&
( $list = $query->get( $key ) ) ) {
global $wpdb;
$list = "'" . implode( wp_parse_list( $list ), "', '" ) . "'";
return "FIELD( $wpdb->postmeta.meta_value, $list )";
}
return $orderby;
}
Step 2
When making your post queries, set the orderby to meta_value_list and add meta_value_list to the query parameters — if you're using get_posts(), make sure suppress_filters is set to false:
$stores = get_posts( array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids', // get just the ID's of posts
'meta_key' => 'state',
'orderby' => 'meta_value_list',
'meta_value_list' => array( 'State1', 'State2', 'State3', '' ),
'suppress_filters' => false,
) );
PS: I the added '' to the array so that posts where the metadata is ('') (i.e. exists in the database, but the value is empty) would be placed at the bottom of the results.
Tried and tested working, but note that the above solution is only for single orderby, which means array is not supported.
/*Display posts of type 'stores', ordered by 'state', and filtered to show only states.*/
$args = array(
'post_type' => 'stores',
'meta_key' => 'state',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'state',
'value' => array( 'State1', 'State2', 'State3'),
'compare' => 'IN',
),
),
);
$query = new WP_Query( $args );
I'm using wordpress to search some content with a few restrictions. Every time I save a post, I save in meta a related posts array.
Then I'm trying to search all posts which has a certain post as related, I have the following query. Selected post is an id to the post I'm looking for.
I checked $selected_post and it has values like the following one.
$selected_post = "25";
I added the meta value using the following instruction, I'm using update_post_meta. $related_posts contains an array of post ids, like the example below.
update_post_meta( $post->ID, 'related_post', $related_posts );
Query
$arg = array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'related_post',
'value' => array( $selected_post ),
'compare' => 'IN',
'type' => 'STRING'
),
),
);
I checked this meta in some of my current posts, and I have something like this.
'related_post' => array( '15', '25', '46' );
When I execute this query using WP_Query it always returns me an empty array. I think I need something more in the query to make this works.
Any help would be appreciated.
The problem is that you are trying to query related_posts as if it was an array, but it actually gets serialised when its added to the database.
Using WP_Query on serialized arrays
If you were to add an array of post meta as follows:
$fruit_array = array( 'apple', 'orange', 'banana' );
update_post_meta( $post->ID, 'fruit', $fruit_array );
...the value for fruit in the database would be:
a:3:{i:0;s:5:"apple";i:1;s:6:"orange";i:2;s:6:"banana";}
Therefore you need to use LIKE to search the serialised string for the value you are looking for.
For the above example, your $args would be something like:
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'fruit',
'value' => 'apple',
'compare' => 'LIKE',
),
)
);
Search for numbers/post ids
In your case, this is complicated by the fact that you are using post ids. Post ids or numbers aren't an issue specifically, but the problem is that LIKE will also return partial matches, so a query for 14 would also return 141, 1455 etc.
Based on the structure of the serialised string, I believe the following should work:
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'related_post',
'value' => '"25"', /* include the double quotes in the search value */
'compare' => 'LIKE',
),
)
);
As the values in the serialised string becomes "25"; including the double quotes in the value you want to search for should work.
I'm trying to query a custom post type 'section' where the 'section.section_parent_page = {whatever the ID of the current $post is}'.
This part of the query works fine and is accomplished like this:
$args = array(
'post_type' => 'section',
'meta_query' => array(
array(
'key' => 'section_parent_page', // name of custom relate field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
);
But once I try to 'orderby' the custom field 'section_position', I either break the query or see no proper ordering. Here's what I currently have:
$args = array(
'post_type' => 'section',
'meta_key' => 'section_position', // custom field I want to order by
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'section_parent_page', // name of custom relate field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
);
When I look at the $the_query->request, it looks like it continues to order by wp_posts.menu_order
How do I use multiple meta_query arrays to accomplish this?
EDIT:
No matter what I try, the end of my query string ends up looking like this:
ORDER BY wp_posts.menu_order ASC LIMIT 0, 10
If section_position has integer ( number ) value, use following code
$args = array('post_type' => 'section',
'orderby' => 'meta_value_num',
'meta_key' => 'section_position',
'order'=>'ASC',
'meta_query' => array(
array(
'key' => 'section_parent_page', // name of custom relate field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
);
Note i have used "meta_value_num" instead of "meta_value"
I attempted to do the same thing recently within an ACF filter. I contacted ACF support and received this answer.
Unfortunately i this query is too complex to be passed through WP_Query and you would have to do a manual SQL query to achieve this.
The problem is you are doing two meta_querys, the meta_key = start date and the ones inside the brackets are conflicting.
You need the flexibility of full SQL syntax to perform a query like that.
So im having some problems getting this query working, im using a meta query to get values that have been stored as a serialized array. Im using the "LIKE" operator like so:
query_posts(array(
'post_type' => 'product',
's' => $keyword,
'meta_query' => array(
array(
'key' => 'raw_data',
'value' => '"'.$collection.'"',
'compare' => 'LIKE'
),
),
$taxQuery => array(
array(
'taxonomy' => 'product_category',
'field' => 'slug',
'terms' => array( $brandOne, $brandTwo, $brandThree )
)),
'posts_per_page' => '24',
'paged' => $paged,
'post_status'=>'publish' ,
'order'=>'ASC',
));
}
But What I need is target a specific key instead of getting the entire table of "raw_data" I need to get something like $raw_data["COLLECT"] and then query the value.
Anything you guys can think of would be great!
your question about how one can query on serialized data has already been discussed in this post, you may find answer there : https://wordpress.stackexchange.com/questions/16709/meta-query-with-meta-values-as-serialize-arrays