Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

php - wp_reset_query not resetting

I am trying to query several different types of post on the same page. When I try to query the second time (Essays), nothing shows up, meaning my if ($arr_posts->have_posts()) is evaluating as false. The first query and the third and fourth query are working fine. And the second query was working until I added this Interview query before it. And even when I commented it out, it still stopped showing up. What am I missing? And mwp_interview is a custom post type.

<!--Latest Interview-->
<?php
$args = array(
    'posts_per_page' => 1,
    'post_status' => 'publish',
    'post_type' => 'mwp_interview',
);
$arr_posts = new WP_Query( $args );

if ( $arr_posts->have_posts() ) :
    $arr_posts->the_post();
    ?>
    <div>
        DISPLAY INTERVIEW POST
    </div>
    <?php
endif; ?>

<!--Latest Essay-->
<?php
wp_reset_query();
wp_reset_postdata();

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => 'Essays in Discipleship',
    'posts_per_page' => 1
);
$arr_posts = new WP_Query( $args );

if ( $arr_posts->have_posts() ) :
    $arr_posts->the_post();
    ?>
    <div>
        DISPLAY ESSAY POST
    </div>
    <?php
endif; ?>

<!--Latest Special Series-->
<?php
$ss_args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => 'Special Post',
    'posts_per_page' => 1,
);
wp_reset_query();
$ss_arr_posts = new WP_Query( $ss_args );

if ( $ss_arr_posts->have_posts() ) :
    $ss_arr_posts->the_post();
    ?>
    <div>
        DISPLAY SPECIAL SERIES POST
    </div>
    <?php
endif;
?>

<!--Latest Podcast-->
<?php
$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => 'Podcast',
    'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );

if ( $arr_posts->have_posts() ) :
    $arr_posts->the_post();
    ?>
    <div>
        DISPLAY PODCAST
    </div>
    <?php
endif;
?>
question from:https://stackoverflow.com/questions/65848828/wp-reset-query-not-resetting

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As per the Wordpress documentation WP_Query Category Parameters.

category_name (string) – use category slug.

The argument's name category_name is actually misleading, category_name is referring to the category SLUG, NOT the actual category NAME.

<?php
$args = [
  'posts_per_page' => 1,
  'post_status' => 'publish',
  'post_type' => 'mwp_interview',
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
  $i = 0;
  while( $query->have_posts() ): $query->the_post();
    $i++;
    if ( $i > 1 )
      echo "<hr>";
    the_title( '<h1>', '</h1>' );
  endwhile;
else:
    echo 'No "interview" just yet!';
endif;
wp_reset_postdata(); ?>

<?php
$args = [
  'post_type' => 'post',
  'post_status' => 'publish',
  'category_name' => 'essays-in-discipleship', // ... must be set set to "Essays In Discipleship" category slug
  'posts_per_page' => 1,
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
  $i = 0;
  while( $query->have_posts() ): $query->the_post();
    $i++;
    if ( $i > 1 )
      echo '<hr>';
    the_title( '<h1>', '</h1>' );
  endwhile;
else:
    echo 'No "Essays In Discipleship" just yet!';
endif;
wp_reset_postdata(); ?>

<?php
$args = [
  'post_type' => 'post',
  'post_status' => 'publish',
  'category_name' => 'special-post', // ... must be set to "Special Post" category slug
  'posts_per_page' => 1,
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
  $i = 0;
  while( $query->have_posts() ): $query->the_post();
    $i++;
    if ( $i > 1 )
      echo '<hr>';
    the_title( '<h1>', '</h1>' );
  endwhile;
else:
    echo 'No "Special Post" just yet!';
endif;
wp_reset_postdata(); ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...