Getting Custom Post Type Data Including ACF With Timber?
I need to get custom post type data for usage with Timber and Twig, it's working fine using the standard WP_Query
, such as:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = new WP_Query( $args );
// Restore the global $post to the current post in the main query
wp_reset_postdata();
However this obviously doesn't get ACF fields.
Was going to do the below as indicated in the Timber docs:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = Timber::get_posts( $args );
However, it seems that get_posts
is getting deprecated in 2.0.
It seems like the best way to do it would be by using Timber's PostQuery
, but because of lacking documentation I am unsure if it is more or less an encapsulation of WP_query
and I can simply do something like:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = new PostQuery(array('query' => $args));
Am I on the right track here or what is the correct way?
Answer
Ok, after finding this answer and also digging through the code a bit I found out how to do it I believe:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$query = new Timber\PostQuery($args);
// Get an array of post objects
$context['posts'] = $query->get_posts();
From what I can tell, all the respective WP_Query
args seems to work using this method as well - but if you need pagination to work I would advise reading that other answer I linked to as it contains more information on that.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?