0

Background: I'm new to the Gutenberg block editor and I'm trying to develop a custom block theme. I'm using ACF to create custom blocks.

Problem: I'm creating custom blocks with the intention of displaying various custom post types. Posts display on the front end. In the Gutenberg page editor, the posts don't display when the block is.

Admin enter image description here Frontend enter image description here

I've determined this is because the have_posts() is returning false and thus, my loop isn't running.

I want to make a nice editing experience, so I need the posts to display in admin editing as well as on the front end.

Question: So I have a couple questions:

  1. Why does have_posts() return false in admin when it returns true on the front-end?
  2. How do I loop through my custom post types in order to get them to display on both the front and backend?

1 Answer 1

0

To get your posts to display in the Gutenberg editor, you'll likely need to create a separate query specifically for your custom block. This is because Gutenberg doesn't use the main query in the same way the front end does.

Something like this:

$args = array(
    'post_type' => 'your_custom_post_type', // Replace with your custom post type
    'posts_per_page' => 10, // Adjust as needed
);

$block_query = new WP_Query($args);

if ($block_query->have_posts()) {
    while ($block_query->have_posts()) {
        $block_query->the_post();
        // Your code to display each post goes here
    }
    wp_reset_postdata();
} else {
    echo '<p>No posts found.</p>';
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.