Get the post type within a Wordpress search loop












2















I'm using a loop in my search.php file to loop through the results and display their title.



This works great, but I'd like to check for each result, whether it is a page or a post. The following code doesn't seem to work.



if ( have_posts() ) : ?>

<h1 class="page-title"><?php printf( __( 'Results for: %s', 'domain' ), '<span>' . get_search_query() . '</span>' ); ?></h1>

<?php while ( have_posts() ) : the_post();

$title = get_the_title();

echo '<article class="post-card">';

if ( is_page() ) {
echo 'page';
} else if ( is_singular('post') ) {
echo 'post';
}

echo '<h2>' .$title. '</h2>';
echo '</article>';

endwhile; ?>

<?php endif;









share|improve this question



























    2















    I'm using a loop in my search.php file to loop through the results and display their title.



    This works great, but I'd like to check for each result, whether it is a page or a post. The following code doesn't seem to work.



    if ( have_posts() ) : ?>

    <h1 class="page-title"><?php printf( __( 'Results for: %s', 'domain' ), '<span>' . get_search_query() . '</span>' ); ?></h1>

    <?php while ( have_posts() ) : the_post();

    $title = get_the_title();

    echo '<article class="post-card">';

    if ( is_page() ) {
    echo 'page';
    } else if ( is_singular('post') ) {
    echo 'post';
    }

    echo '<h2>' .$title. '</h2>';
    echo '</article>';

    endwhile; ?>

    <?php endif;









    share|improve this question

























      2












      2








      2








      I'm using a loop in my search.php file to loop through the results and display their title.



      This works great, but I'd like to check for each result, whether it is a page or a post. The following code doesn't seem to work.



      if ( have_posts() ) : ?>

      <h1 class="page-title"><?php printf( __( 'Results for: %s', 'domain' ), '<span>' . get_search_query() . '</span>' ); ?></h1>

      <?php while ( have_posts() ) : the_post();

      $title = get_the_title();

      echo '<article class="post-card">';

      if ( is_page() ) {
      echo 'page';
      } else if ( is_singular('post') ) {
      echo 'post';
      }

      echo '<h2>' .$title. '</h2>';
      echo '</article>';

      endwhile; ?>

      <?php endif;









      share|improve this question














      I'm using a loop in my search.php file to loop through the results and display their title.



      This works great, but I'd like to check for each result, whether it is a page or a post. The following code doesn't seem to work.



      if ( have_posts() ) : ?>

      <h1 class="page-title"><?php printf( __( 'Results for: %s', 'domain' ), '<span>' . get_search_query() . '</span>' ); ?></h1>

      <?php while ( have_posts() ) : the_post();

      $title = get_the_title();

      echo '<article class="post-card">';

      if ( is_page() ) {
      echo 'page';
      } else if ( is_singular('post') ) {
      echo 'post';
      }

      echo '<h2>' .$title. '</h2>';
      echo '</article>';

      endwhile; ?>

      <?php endif;






      php wordpress loops post search






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 18:57









      Quentin VeronQuentin Veron

      1,3631221




      1,3631221
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I think you are looking for this function. Will be shorter. Just pass the post object and it will return string containing the post type.



          get_post_type( int|WP_Post|null $post = null )



           $somePost; // post
          $postType = get_post_type($somePost);

          if ('page' === $postType) {
          // page...
          }
          elseif ('post' === $postType {
          // post...
          }





          share|improve this answer































            0














            This will get post type in the loop.



            if ( 'page' == get_post_type( get_the_ID() )



            Best wishes,



            Mitchell






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998101%2fget-the-post-type-within-a-wordpress-search-loop%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              I think you are looking for this function. Will be shorter. Just pass the post object and it will return string containing the post type.



              get_post_type( int|WP_Post|null $post = null )



               $somePost; // post
              $postType = get_post_type($somePost);

              if ('page' === $postType) {
              // page...
              }
              elseif ('post' === $postType {
              // post...
              }





              share|improve this answer




























                0














                I think you are looking for this function. Will be shorter. Just pass the post object and it will return string containing the post type.



                get_post_type( int|WP_Post|null $post = null )



                 $somePost; // post
                $postType = get_post_type($somePost);

                if ('page' === $postType) {
                // page...
                }
                elseif ('post' === $postType {
                // post...
                }





                share|improve this answer


























                  0












                  0








                  0







                  I think you are looking for this function. Will be shorter. Just pass the post object and it will return string containing the post type.



                  get_post_type( int|WP_Post|null $post = null )



                   $somePost; // post
                  $postType = get_post_type($somePost);

                  if ('page' === $postType) {
                  // page...
                  }
                  elseif ('post' === $postType {
                  // post...
                  }





                  share|improve this answer













                  I think you are looking for this function. Will be shorter. Just pass the post object and it will return string containing the post type.



                  get_post_type( int|WP_Post|null $post = null )



                   $somePost; // post
                  $postType = get_post_type($somePost);

                  if ('page' === $postType) {
                  // page...
                  }
                  elseif ('post' === $postType {
                  // post...
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 19:04









                  revengeancerevengeance

                  633215




                  633215

























                      0














                      This will get post type in the loop.



                      if ( 'page' == get_post_type( get_the_ID() )



                      Best wishes,



                      Mitchell






                      share|improve this answer




























                        0














                        This will get post type in the loop.



                        if ( 'page' == get_post_type( get_the_ID() )



                        Best wishes,



                        Mitchell






                        share|improve this answer


























                          0












                          0








                          0







                          This will get post type in the loop.



                          if ( 'page' == get_post_type( get_the_ID() )



                          Best wishes,



                          Mitchell






                          share|improve this answer













                          This will get post type in the loop.



                          if ( 'page' == get_post_type( get_the_ID() )



                          Best wishes,



                          Mitchell







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 1 at 21:10









                          WhereDidMyBrainGoWhereDidMyBrainGo

                          12118




                          12118






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998101%2fget-the-post-type-within-a-wordpress-search-loop%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith