I have a query in a custom widget (my first one!) which, if it's of custom post type
book, retrieves the post ID. This then gets used in a WP_QUERY to retrieve data about the page.
This works perfectly when in the static sidebar.php, but not in either a dynamic PHP Widget or as a custom widget in functions.php. In those, it retrieves the relevant field from all examples of post type
Here's the offending code.
<?php if('book' == get_post_type()) : $ww_book_id = $post->ID; else : $ww_book_id = $default_book; //this is pulled in from the widget admin options form and works perfectly. endif; $testimonial_args = array( 'post_type' => 'book', 'p' => $ww_book_id ); $main_testimonials = new WP_Query($testimonial_args); if($main_testimonials->have_posts()) : while($main_testimonials->have_posts()): $main_testimonials->the_post(); echo the_title(); //I actually do much more stuff than this, but it works for testing purposes. endwhile; wp_reset_postdata(); endif; ?>