Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Issue displaying custom post type using WP_Query()

Featured Replies

<?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??

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 by teodora

  • 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;'
  • 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??

  • 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.

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.