Not quite sure what you want. Can you give an example?
You can use a WP_Query to search the custom post type of “car” and use the slug of a specific category.
Thank you for your feed-back.
Let me try to explain better :
– I have defined 2 custom posts : “car” and “car library”
– “car” and “car library” contains both a listbox enabling to select one category for the “car” custom posts and several categories for the “car library” custom post : the category is here to make the link between both “car” and “car library” custom posts
So i have 1 post for each car description but one “car library” item can be used for several cars description – e.g. 3.8 motor description in “car library” is the same for car A and car B.
I would like to use WP_query to search all the “car library” posts where “car_library-category” (array as multiple selection possible) contains “car-category”
–> Could you provide me an example how to write this ith WP_Query ?
I hope it’s a bit more understandable 😉
Thank you in advance for your help
regards,
As far as I can tell, ‘car-category’ will be the slug of a category. It doesn’t matter that car_library-category is an array as long as the values are stored as real categories. If not, you need to explain how the values are stored.
$cat_slug = 'the-category-slug'; // The value of 'car-category'
$args = array(
'post_type' => 'car-library',
'posts_per_page' => -1,
'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'car_library-category',
'field' => 'slug',
'terms' => $cat_slug
),
);
$results = new WP_Query($args);
Hello,
Thank you for your quick answer but i don’t think this works as i’m not using taxonomies but wordpress categories id’s in my custom posts.
When i have a look in the post_meta table for the field “car-category” in a “car” custom post type, i have e.g. this : a:1:{i:0;s:4:”2198″;}.
When i have a look in the post-meta table fot the field “car-library-category” in a “car library” custom post type (with multiple categories selected), i have e.g. this : a:3:{i:0;s:4:”1992″;i:1;s:4:”1994″;i:2;s:4:”2198″;…
—> in the exemple above, the ID 2198 matches –> so this library item can be displayed for this car.
Hope these details can help to solve my problem.
Thank you in advance for your support.
regards,
It appears that you are using Custom Fields, probably with a plugin like Advanced Custom Fields, to store the category IDs. The values are being stored in serialized strings and MySQL does not have the ability to extract/search for individual values in those strings.
If you have the ID of the car record, you may be able to use something like this (untested):
$car_cat_string = get_post_meta($car_id, 'car-category', true);
if ( $car_cat_string ) {
$car_cat_array = unserialize($car_cat_string);
$car_cat = $car_cat_array[0];
$args = array(
'post_type' => 'car-library',
'posts_per_page' => -1,
'ignore_sticky_posts' => 1,
'meta_query' => array(
array(
'key' => 'car_library-category',
'value' => '"' . $car_cat . '"',
'compare' => 'LIKE'
),
)
);
$results = new WP_Query($args);
if ( $results->have_posts() ) {
while ( $results->have_posts() ) {
$results->the_post();
// Your output code here
}
}
}
Great, this way it works fine (with some changes) 🙂
Thank you for your Help !