May 30, 201313 yr <?php $args = new WP_Query(array( 'post-type' => 'homepage-slides', 'posts_per_page' => -1 )); while ($args->have_posts()) : $args->the_post(); the_title(); ?> <?php endwhile; ?> The code above (for now) is supposed to display the titles of my custom posts 'homepage-slides' However the query does not seem to be targeting the post type and instead displaying the titles for all the blog posts. I've checked the code over and over for syntax errors and misspelling of the post type - everything looks as it should. I cannot understand why this custom post type is not being targeted??
May 30, 201313 yr What about: $args = array( 'post-type' => 'homepage-slides', 'posts_per_page' => -1, ); $wp_query = new WP_Query($args); while ($wp_query->have_posts()) : $wp_query->the_post(); Edited May 30, 201313 yr by teodora
May 30, 201313 yr Then you can write whatever you need to have in the loop after while ($wp_query->have_posts()) : $wp_query->the_post();
May 30, 201313 yr Author Thanks for your response, I just tried it and the exact same is still happening. There is a custom field in this custom post type called 'image' that I've also tried to display but nothing. comes up in the html source when viewed in the browser: <?php the_field('image'); ?> Just for reference that I haven't missplaced the above line, in my source it's located just before the 'endwhile;'
May 30, 201313 yr Author What is confusing me is that the post-type argument sent to the API is being ignored completely I've tried just putting random data into the post type request: 'post-type' => 'blah' Still it returns the blog posts, surely that should return null or an error??
May 30, 201313 yr Hmmm, don't know... You have double checked the custom post name, haven't you? Do you have other loops in the page? I usually show the custom fields using get_post_meta(); http://codex.wordpress.org/Function_Reference/get_post_meta
May 30, 201313 yr Author There is another loop that's configured to display the post title of the latest post. That works ok. I just removed that to see if it's causing any conflicts and the WP_query is still displaying the blog posts instead of the custom post despite being the only loop.
May 30, 201313 yr Author This seems to be working now.. weird... <?php $the_query = new WP_Query(array( 'post_type' => 'homepage-slides', 'posts_per_page' => -1 )); while ($the_query->have_posts()) : $the_query->the_post(); ?> <?php the_title(); ?> <?php the_field('image'); ?> <?php endwhile; ?> I can't see why the others don't work but this does. But at least it's working now I am sure I've gained a few grey hairs over that!
Create an account or sign in to comment