Skip Navigation

[Resolved] WP_Query custom post type and custom field

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 1 reply, has 2 voices.

Last updated by Mateus Getulio 1 year, 1 month ago.

Assisted by: Mateus Getulio.

Author
Posts
#2661021

Hi,
I created a custom post type "auto". In this custom post type I have custom fields such as for example "model" and "type". How can I create a WP_Query to filter based on my fields?
Regards

#2661281

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

Thank you for contacting our support.

To create a WP_Query to filter your custom post type "auto" based on custom fields like "model" and "type," you can use the 'meta_query' parameter. Here's an example of how you can construct such a query:

$args = array(
    'post_type' => 'auto', // Replace 'auto' with your custom post type name
    'posts_per_page' => -1, // Use -1 to retrieve all matching posts, or specify a number
    'meta_query' => array(
        'relation' => 'AND', // You can use 'AND' or 'OR' as per your filtering requirements
        array(
            'key' => 'model', // Replace 'model' with your custom field key
            'value' => 'desired_model_value', // Replace 'desired_model_value' with the value you want to filter by
            'compare' => '=', // You can use different comparison operators like '=', '!=', '>', '<', 'LIKE', etc.
            'type' => 'CHAR', // Adjust the data type if necessary
        ),
        array(
            'key' => 'type', // Replace 'type' with your custom field key
            'value' => 'desired_type_value', // Replace 'desired_type_value' with the value you want to filter by
            'compare' => '=', // You can use different comparison operators like '=', '!=', '>', '<', 'LIKE', etc.
            'type' => 'CHAR', // Adjust the data type if necessary
        ),
    ),
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        // Your loop code here
        // You can access post data using functions like the_title(), the_content(), etc.
    }
    wp_reset_postdata(); // Restore the original post data
} else {
    // No posts found
}

In the code above you have to:

- Replace 'auto' with your custom post type name.
- Replace 'model' and 'type' with the keys of your custom fields.
- Adjust the 'value' and 'compare' parameters as per your filtering criteria.
- You can use the 'type' parameter to specify the data type of your custom field if it's not a standard string.

This code will create a WP_Query that retrieves posts from the "auto" custom post type based on the specified custom field values. You can further customize this query to suit your specific needs.

For more info on how to use the WP_Query please check our tutorial: https://toolset.com/glossary/wp_query/

If you find any other concerns, feel free to inform us.

Best regards,

Mateus