I am trying to accomplish this without the use of multiple queries. Is it possible ?
I have a custom post type set up with custom fields (Using advanced custom fields).
Here is my scenario as is now. (demo post type and fields so you get the idea)
'post_type' => 'employee',
'post_status' => 'any',
'meta_query' => array(
array(
'key' => 'position',
'value' => 'driver',
'compare' => '='
)
),
This will print as:
Driver
Image
Name of one driver
Contact info
And then a new loop here to filter out other position. Lets say managers.
'post_type' => 'employee',
'post_status' => 'any',
'meta_query' => array(
array(
'key' => 'position',
'value' => 'manager',
'compare' => '='
)
),
Managers
Image
Name of manager
Contact info.
So my question is this, can i make one loop print header for each value of meta_query one time and list the children under it ?
Or am i stuck with multiple loops ?
Hey By using get_post_meta() you can simply fetch the meta value
Example-
$key_value = get_post_meta( get_the_ID(), 'key', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
}
I think it work fine.
Related
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 am using Woocommerce, although my question relates to WP_Query in general. I am creating a custom WP_Query loop for bestselling products and I want to allow users to be able to sort the results by price, from high to low and low to high. The problem is in Woocommerce both _price and total_sales are meta fields and as far as I can tell I can only orderby 1 meta field in Wordpress loops. Is there a way around this?
My full code including some of my attempts is here, the most relevant code segment looks like as follows:
$queryArgs = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'meta_key' => '_price',
'orderby' => array('meta_value_num' => $sortBy['terms'])
);
The actual code is more complicated than this because it's built for a range of filtering and sorting options, but this is the gist of it and bestsellers is the only thing giving me issues because of the two meta_keys. I've read this but it doesn't apply to custom meta fields.
I've tried:
'meta_key' => '_price total_sales'
'meta_key => array('_price', 'total_sales')
'orderby' => array('meta_value_num' => $sortBy['terms'], 'meta_value_num' => 'DESC')
Nothing seems to work. I've also tried hooking onto the various WP_Query filters but the problem is this query is part of a dynamically generated loop so I can't 'hack' or hard-code anything.
Use the meta query with arrays:
'meta_query' => array (
array (
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
),
array (
'key' => '_price',
'value' => "VALUE WHAT YOU WANT, NOT ASC/DESC",
)
),
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.
I'm trying to set up a custom query that returns only posts that do not have a custom meta field checkbox is checked. The checkbox meta field id is position-filled.
Here is the code I have:
$args = array (
'post_type' => 'jobs',
'posts_per_page' => 4,
'post__not_in' => array(get_the_id()),
'meta_query' => array(
array(
'key' => 'position-filled',
'value' => 'on', // also tried passing null and removing 'compare'
'compare' => '!=' // also tried 'NOT LIKE'
)
),
);
$customQuery = new WP_Query($args);
However, this returns no posts (I have made certain there are posts without the checkbox checked.
Any idea what I'm doing wrong?
I've run into similar issues when adding custom fields to existing posts/pages where I needed to search on "unchecked" or "unselected" fields.
It boils down to the fact that the query is actually saying, "Give me posts where the meta key (position-filled) is populated, but not populated with a value of 'on'".
Does it work if you re-save your post with the checkbox being unchecked?
If so, you might also try using the 'compare' value of 'NOT EXISTS'. Although I believe if you use NOT EXISTS and then someone saves an older post without the checkbox being checked, it will exist (with a blank value).
[edit]
I forgot to mention that you can build a query with either situation being true (the NOT EXISTS or it does exist but the value is blank/unchecked).
$args = array(
'post_type' => 'jobs',
'posts_per_page' => 4,
'post__not_in' => array(get_the_id()),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'position-filled',
'value' => 'on',
'compare' => '!='
),
array(
'key' => 'position-filled',
'value' => '',
'compare' => 'NOT EXISTS'
)
)
);
You can chain multiple comparisons together and using the relation property of OR, either one should return true.
I am using Advanced Custom Fields plugin with WordPress. I am using relationship custom field.
$posts = get_posts(array(
'post_type' => 'slos_trainings',
'meta_query' => array(
array(
'key' => 'lokacija', // name of custom field
'value' => 79,
'compare' => 'LIKE'
)
)
));
I would like to pass array into meta_query value. Currently my code is working and it is returning all custom post types slos_trainings that have location custom meta field set to 79. But this field is a relationship, so it can have multiple ID-s. For example I want to find all posts with type slos_trainings that have lokacija set to 79 OR 200 OR 124, etc,..
How can I do this?
$args = array(
'post_type' => 'slos_trainings',
'meta_query' => array(
array(
'key' => 'lokacija',
'value' => array ( 79, 200, 124),
'compare' => 'IN'
)
)
);