$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How do you determine if a result in a search query is a post or a page?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How do you determine if a result in a search query is a post or a page?

matteradmin7PV0评论

I thought this question would be an easy find on the internet… however, it seems no one has ever ran into this issue before.

There's no problem in listing a search query when you enter your search term, I needed posts and pages to be differentiated in a way. For example: I'd like results that are posts (articles) to display the author, read time, and post date, but I do not like these metas to appear on results that are pages.

Here's a code:

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $searchArgs = array(
    's' => $s,
    'paged' => $paged,
    'showposts' => -1,
    );
    $searchQuery = new WP_Query( $searchArgs );
    if ( $searchQuery->have_posts() ) :
    echo '<h2>';
    echo 'result: '.search_query();
    echo '</h2>';
    while ( $searchQuery->have_posts() ) : $searchQuery->the_post();
      echo 'link: '.get_the_permalink();
      echo 'Title: '.get_the_title();
      echo 'Post Summary: '.get_the_excerpt();
      $how = ( is_single() ) ? 'this result is a post' : 'your method did not work'; //always return false
      $how = ( is_page() ) ? 'this result is a page' : 'your method did not work'; //always return false.
      echo 'Post Type: '.$how; // <------ How to do?
      //***Problem***: is_single() or is_page() does not work, by the way (both return false).
      //I've crossed-checked that the results are actually either a post or a page.
    endwhile;
    endif;

if ( function_exists('custom_pagination') ) {
      custom_pagination($searchQuery->max_num_pages,"",$paged);
    }

So my problem is, is_page() and is_single() does not work within the Search Query I made.

How do you do this the right way?

I thought this question would be an easy find on the internet… however, it seems no one has ever ran into this issue before.

There's no problem in listing a search query when you enter your search term, I needed posts and pages to be differentiated in a way. For example: I'd like results that are posts (articles) to display the author, read time, and post date, but I do not like these metas to appear on results that are pages.

Here's a code:

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $searchArgs = array(
    's' => $s,
    'paged' => $paged,
    'showposts' => -1,
    );
    $searchQuery = new WP_Query( $searchArgs );
    if ( $searchQuery->have_posts() ) :
    echo '<h2>';
    echo 'result: '.search_query();
    echo '</h2>';
    while ( $searchQuery->have_posts() ) : $searchQuery->the_post();
      echo 'link: '.get_the_permalink();
      echo 'Title: '.get_the_title();
      echo 'Post Summary: '.get_the_excerpt();
      $how = ( is_single() ) ? 'this result is a post' : 'your method did not work'; //always return false
      $how = ( is_page() ) ? 'this result is a page' : 'your method did not work'; //always return false.
      echo 'Post Type: '.$how; // <------ How to do?
      //***Problem***: is_single() or is_page() does not work, by the way (both return false).
      //I've crossed-checked that the results are actually either a post or a page.
    endwhile;
    endif;

if ( function_exists('custom_pagination') ) {
      custom_pagination($searchQuery->max_num_pages,"",$paged);
    }

So my problem is, is_page() and is_single() does not work within the Search Query I made.

How do you do this the right way?

Share Improve this question edited Nov 13, 2018 at 17:52 vm7488 asked Nov 13, 2018 at 17:45 vm7488vm7488 331 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

That will only tell you if it's a Post or a Page. What if it's a category, tag, archive, or custom post type? Here's how I'd write that, if you care.

function xyz_get_post_type_name() {
    $wp_type = get_post_type( get_the_ID()  );
    switch ($wp_type) {
        case 'post' :
            $type_name = 'Article';
        break;
        case 'page' :
            $type_name = 'Web Page';
        break;
        case 'quote' :
            $type_name = 'Testimonial';
        break;
        case 'post_tag' :
            $type_name = 'Topic';
        break;
        default : 
            $type_name = ucfirst($wp_type);
        break;
    } // END switch
    return $type_name;
} // END xyz_get_post_type_name()

Then just echo that function inside your loop wherever you want it.

while ( $searchQuery->have_posts() ) : $searchQuery->the_post();
    echo 'Post Type Name: ' . xyz_get_post_type_name();
endwhile;

The switch statement would also allow you to give these post types a "pretty" name and default to just their actual name (first letter capitalized).

…Figured it out… 5 minutes later…

    $queryIsPost = (get_post_type() === 'post') ? true : false; //the solution
    $queryIsPage = (get_post_type() === 'page') ? true : false; //the solution

    $searchArgs = array(
    's' => $s,
    'paged' => $paged,
    'showposts' => -1,
    );
    $searchQuery = new WP_Query( $searchArgs );
    if ( $searchQuery->have_posts() ) :
    echo '<h2>';
    echo 'result: '.search_query();
    echo '</h2>';
    while ( $searchQuery->have_posts() ) : $searchQuery->the_post();
      echo 'link: '.get_the_permalink();
      echo 'Title: '.get_the_title();
      echo 'Post Summary: '.get_the_excerpt();
      if ($queryIsPost) {
        $thePostType = "Post";
      } elseif ($queryIsPage) {
        $thePostType = "Page";
      } else {
        $thePostType = "Error. Not determined";
      }
      echo 'Post Type: '.$thePostType;

    endwhile;
    endif;
Post a comment

comment list (0)

  1. No comments so far