Frank Lucas
Frank Lucas

Reputation: 592

WordPress query custom fields of a custom post type

I have a custom post type 'orders' and there are a few custom fields attached to this post type.

When I query all orders like so:

$orders = new WP_Query( array( 'post_type' => 'orders') );

I don't get all the data I wish to get.

I have a tab custom field with a repeater in it. And I kinda need the data from the repeater. Anyone knows why I don't get this data in the dump of $orders?

Should my query be something else to get all this data?

EDIT

I've managed to query this array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [attractie] => WP_Post Object
                        (
                            [ID] => 41
                            [post_author] => 1
                            [post_date] => 2016-02-29 14:30:33
                            [post_date_gmt] => 2016-02-29 14:30:33
                            [post_content] => <h2>Title!</h2>
                            Content.
                            [post_title] => Post Title
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => post-name
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2016-04-07 14:39:41
                            [post_modified_gmt] => 2016-04-07 14:39:41
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => url
                            [menu_order] => 0
                            [post_type] => attracties
                            [post_mime_type] => 
                            [comment_count] => 0
                            [filter] => raw
                        )

                    [start] => 0930
                    [end] => 1200
                )

            [1] => Array
                (
                    [attractie] => 
                    [start] => 0930
                    [end] => 1200
                )

        )

With this code:

$orders = new WP_Query( array( 'post_type' => 'orders') );

foreach ( $orders->posts as $all ) {
   $fields[] = get_field( 'planning', $all->ID );
}

What I basically need now is the post_title value from the attractie WP_Post Object and the start and end values.

Upvotes: 0

Views: 4363

Answers (3)

user2917245
user2917245

Reputation:

So far so good, you are already creating custom query:

$loopargs = array( //in the array are arguments for custom query            
   'post_type' => 'orders', //your CPT
   'posts_per_page' => -1, //lets make WP show them all
   'orderby' => 'date', // newest
   'order' => 'DESC', //to the top
); 
$query = new WP_Query($loopargs);

Now let us loop through it:

while ($query->have_posts()) : $query->the_post(); //the_post loads for you current post in loop so that you can use functions like the_title(), the_content(), etc..

about ACF Plugin:

the_field('your_field_name'); //will output field content directly get_field('your_field_name'); //returns value from your custom field get_fields(); //returns array of your fields

Note that those ACF plugin functions will work in loop only, in our case when we set up the post with the_post() in while loop everytime. Otherwise you need to pass POST_ID as second (in first two) and as first parameters in ACF functions.

Also not that we have to reset your query after every custom one. And of course to end up our loop.

endwhile; 
wp_reset_query();

References

Class Reference/WP Query

Function Reference/the post

ACF Documentation

Wordpress The Loop

EDIT

To access elements in your array from question you would do this:

$title = $array[0][0]['attractie']->post_title;
$start = $array[0][0]['start'];
$end = $array[0][0]['end'];

But this is really NOT the way you should do it.

Upvotes: 1

Naisa purushotham
Naisa purushotham

Reputation: 913

use get_post_custom function for getting list of custom Fields

get_post_custom($post_id);

Upvotes: 0

Owais Aslam
Owais Aslam

Reputation: 1589

There is a method to get acf fields try using like this

$orders = new WP_Query( array( 'post_type' => 'orders') );
foreach ( $orders->posts as $allPosts ) {
$field     = get_field( 'your field name', $allPosts->ID );
}

Upvotes: 0

Related Questions