Query posts get post data

You can use this to access the the WordPress posts.

 //Reading posts for “Events” category;
    $posts = get_posts(“category=” . get_cat_ID(“Events”) .’&orderby=date&order=ASC&numberposts=50′);

Then you can create an array and access the post data like this.

//Creating drop down item array.
    $items = array();

    //Adding initial blank value, for a dropdown, or just to make the (zero) position empty
    $items[] = array(“text” => “Select and Event”, “value” => “”);

    //Adding post titles to the items array. Also the Tour Code from the custom field tour_code
    foreach($posts as $post)
        $items[] = array(“text” => $post->post_title, “value” => get_field(‘tour_code’, $post->ID) . ‘-‘ . $post->ID);

You can use print_r($items); to view the array data in your browser.

For post content use $post->post_content

Leave a Reply