0

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.

3
  • Why did you tag advanced-custom-fields ? Are you using the plugin to attach some fields to your CPT ? Commented Apr 13, 2016 at 12:04
  • Yes I am using the ACF plugin to create my custom fields. Commented Apr 13, 2016 at 12:10
  • Check the answer. There are API's either in WP as well as in AFC and CPT UI plugins. Commented Apr 13, 2016 at 12:22

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Hello friend, I've updated OP with the code I am currently using. Check it out if you want.
I understand but do you have any idea how I can get those 3 values I explained in OP? the post_title of the attractie post opbject and the start and end value?
I bet start and end are custom fields right? If you would read the answer you could find out that everything can be done with one method get_field('start') inside loop, the thing you are missing in your code. Also please read Wordpress Codex.
I've tried this it works for start but doesn't for end, weirdly enough :/
Could you please show us how you did it? What works and what dont?
|
0

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 );
}

5 Comments

What should I fill in here:'your field name'? It's a bit confusing since I have a tab field purchases with repeater agenda in it and in the repeater a post object name. And I need the name value
add your repeater field name here.
so $field will give me an array of every value for every order?
you will get array of your repeater fields
0

use get_post_custom function for getting list of custom Fields

get_post_custom($post_id);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.