1

I have a custom page template that includes a basic query after the main content. like this:

$myPosts = new WP_Query(
  array(
    'post_type' => array('post', 'contentTypeA', 'contentTypeB' ),
    'posts_per_page' => 10,
    'order' => 'DESC',
  )
);

if ( $myPosts->have_posts() ) :
  while ( $myPosts->have_posts() ) : $myPosts->the_post();
    get_template_part( 'template-parts/content', get_post_type() );
  endwhile;
endif;
wp_reset_postdata();

My problem is that the results think that they are single pages. The template part linked by get_template_part() has a condition that if is_singular() is true, among many other things, render the title as <h1>, otherwise render as <h2>.

In my WP_Query feed, all the results are using <h1> for titles because is_singular() is registering as true for the posts.

So, my question is: is there an argument I can pass to my WP_Query to force the results to think that is_singular() is false? In other words, to make this output like a standard archive page.

I have discovered that if I use if ( is_singular() && in_the_loop() ) : it sort of does the trick, but I am hoping there is a much simpler solution than adding that to the dozens of template partials I have that rely on is_singular().

1 Answer 1

0

If you really think the is_singular() function of your query should be returning true for singular but isn't, you can force it to be true by setting the public property is_singular directly after the query, since that function just returns the value of that property when no $post_types parameter is specified:

$the_query = new WP_Query($args);
$the_query->is_singular = true;

Or if you need to do so for the main query:

global $wp_query;
$wp_query->is_singular = true;

I can't speak to the wisdom of whether or not this should be done—it probably depends largely on your implementation—but it can be done since that property is public.

Your Answer

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

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