How to search for a string in a category name?











up vote
0
down vote

favorite












I use the search result and I GET



$s = $_GET['s'];  


Which gives: "mario"



Now there is a category named Mario Bianchi under a main category called Persone



When I GET $s I need to get all posts within that category, I tried the following but I get nothing



$terms = get_terms( 'category', array(
'name__like' => $s,
'hide_empty' => true // Optional
) );
if ( count($terms) > 0 ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li><a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a></li>';
}
echo '</ul>';
}


Yet I need the actual post attached, not the category itself










share|improve this question




























    up vote
    0
    down vote

    favorite












    I use the search result and I GET



    $s = $_GET['s'];  


    Which gives: "mario"



    Now there is a category named Mario Bianchi under a main category called Persone



    When I GET $s I need to get all posts within that category, I tried the following but I get nothing



    $terms = get_terms( 'category', array(
    'name__like' => $s,
    'hide_empty' => true // Optional
    ) );
    if ( count($terms) > 0 ){
    echo '<ul>';
    foreach ( $terms as $term ) {
    echo '<li><a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a></li>';
    }
    echo '</ul>';
    }


    Yet I need the actual post attached, not the category itself










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I use the search result and I GET



      $s = $_GET['s'];  


      Which gives: "mario"



      Now there is a category named Mario Bianchi under a main category called Persone



      When I GET $s I need to get all posts within that category, I tried the following but I get nothing



      $terms = get_terms( 'category', array(
      'name__like' => $s,
      'hide_empty' => true // Optional
      ) );
      if ( count($terms) > 0 ){
      echo '<ul>';
      foreach ( $terms as $term ) {
      echo '<li><a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a></li>';
      }
      echo '</ul>';
      }


      Yet I need the actual post attached, not the category itself










      share|improve this question















      I use the search result and I GET



      $s = $_GET['s'];  


      Which gives: "mario"



      Now there is a category named Mario Bianchi under a main category called Persone



      When I GET $s I need to get all posts within that category, I tried the following but I get nothing



      $terms = get_terms( 'category', array(
      'name__like' => $s,
      'hide_empty' => true // Optional
      ) );
      if ( count($terms) > 0 ){
      echo '<ul>';
      foreach ( $terms as $term ) {
      echo '<li><a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a></li>';
      }
      echo '</ul>';
      }


      Yet I need the actual post attached, not the category itself







      php wordpress






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 at 1:53

























      asked Nov 15 at 1:48









      rob.m

      3,639103782




      3,639103782
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          get_terms does just that, it get the terms for your query. What you'll want is WP_Query with a tax_query. You've already got the categories you want to return the posts for, so it shouldn't be too difficult



          $terms = get_terms( array(
          'taxonomy' => 'category',
          'name__like' => $s,
          'hide_empty' => true // Optional
          ) );

          $term_ids = array();

          if ( ! empty( $terms ) ) {
          foreach( $terms as $term ) {
          $term_ids = $term->term_id;
          }

          $args = array(
          'post_type' => 'post',
          'tax_query' => array(
          array(
          'taxonomy' => 'category',
          'terms' => $term_ids,
          ),
          ),
          );

          $query = new WP_Query( $args );

          if ( $query->have_posts() ) {
          echo '<ul>';
          while ( $query->have_posts() ) {
          $query->the_post();
          echo '<li><a href="' . esc_url( get_the_permalink() ) . '" title="' . get_the_title() . '">' . esc_html( get_the_title() ) . '</a></li>';
          }
          echo '</ul>';
          }
          wp_reset_postdata();

          }





          share|improve this answer























          • ok let me try this, p.s. syntax error, unexpected '{' on if ( ! empty( $terms ) {
            – rob.m
            Nov 15 at 2:05










          • it is not looking in category, only finds a match of $s in a title, but let's say I search for mario and I have a category called mario bianchi, I need all posts within that category since we found a match of $s (mario) in a category since we have one mario bianchi
            – rob.m
            Nov 15 at 2:07












          • if I do $term_ids = array(); var_dump($terms); i get array(0) {} but we do have a category called mario bianchi
            – rob.m
            Nov 15 at 2:14










          • @rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
            – cameronjonesweb
            Nov 15 at 2:23










          • in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
            – rob.m
            Nov 15 at 2:24













          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',
          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%2f53311303%2fhow-to-search-for-a-string-in-a-category-name%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          get_terms does just that, it get the terms for your query. What you'll want is WP_Query with a tax_query. You've already got the categories you want to return the posts for, so it shouldn't be too difficult



          $terms = get_terms( array(
          'taxonomy' => 'category',
          'name__like' => $s,
          'hide_empty' => true // Optional
          ) );

          $term_ids = array();

          if ( ! empty( $terms ) ) {
          foreach( $terms as $term ) {
          $term_ids = $term->term_id;
          }

          $args = array(
          'post_type' => 'post',
          'tax_query' => array(
          array(
          'taxonomy' => 'category',
          'terms' => $term_ids,
          ),
          ),
          );

          $query = new WP_Query( $args );

          if ( $query->have_posts() ) {
          echo '<ul>';
          while ( $query->have_posts() ) {
          $query->the_post();
          echo '<li><a href="' . esc_url( get_the_permalink() ) . '" title="' . get_the_title() . '">' . esc_html( get_the_title() ) . '</a></li>';
          }
          echo '</ul>';
          }
          wp_reset_postdata();

          }





          share|improve this answer























          • ok let me try this, p.s. syntax error, unexpected '{' on if ( ! empty( $terms ) {
            – rob.m
            Nov 15 at 2:05










          • it is not looking in category, only finds a match of $s in a title, but let's say I search for mario and I have a category called mario bianchi, I need all posts within that category since we found a match of $s (mario) in a category since we have one mario bianchi
            – rob.m
            Nov 15 at 2:07












          • if I do $term_ids = array(); var_dump($terms); i get array(0) {} but we do have a category called mario bianchi
            – rob.m
            Nov 15 at 2:14










          • @rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
            – cameronjonesweb
            Nov 15 at 2:23










          • in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
            – rob.m
            Nov 15 at 2:24

















          up vote
          0
          down vote



          accepted










          get_terms does just that, it get the terms for your query. What you'll want is WP_Query with a tax_query. You've already got the categories you want to return the posts for, so it shouldn't be too difficult



          $terms = get_terms( array(
          'taxonomy' => 'category',
          'name__like' => $s,
          'hide_empty' => true // Optional
          ) );

          $term_ids = array();

          if ( ! empty( $terms ) ) {
          foreach( $terms as $term ) {
          $term_ids = $term->term_id;
          }

          $args = array(
          'post_type' => 'post',
          'tax_query' => array(
          array(
          'taxonomy' => 'category',
          'terms' => $term_ids,
          ),
          ),
          );

          $query = new WP_Query( $args );

          if ( $query->have_posts() ) {
          echo '<ul>';
          while ( $query->have_posts() ) {
          $query->the_post();
          echo '<li><a href="' . esc_url( get_the_permalink() ) . '" title="' . get_the_title() . '">' . esc_html( get_the_title() ) . '</a></li>';
          }
          echo '</ul>';
          }
          wp_reset_postdata();

          }





          share|improve this answer























          • ok let me try this, p.s. syntax error, unexpected '{' on if ( ! empty( $terms ) {
            – rob.m
            Nov 15 at 2:05










          • it is not looking in category, only finds a match of $s in a title, but let's say I search for mario and I have a category called mario bianchi, I need all posts within that category since we found a match of $s (mario) in a category since we have one mario bianchi
            – rob.m
            Nov 15 at 2:07












          • if I do $term_ids = array(); var_dump($terms); i get array(0) {} but we do have a category called mario bianchi
            – rob.m
            Nov 15 at 2:14










          • @rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
            – cameronjonesweb
            Nov 15 at 2:23










          • in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
            – rob.m
            Nov 15 at 2:24















          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          get_terms does just that, it get the terms for your query. What you'll want is WP_Query with a tax_query. You've already got the categories you want to return the posts for, so it shouldn't be too difficult



          $terms = get_terms( array(
          'taxonomy' => 'category',
          'name__like' => $s,
          'hide_empty' => true // Optional
          ) );

          $term_ids = array();

          if ( ! empty( $terms ) ) {
          foreach( $terms as $term ) {
          $term_ids = $term->term_id;
          }

          $args = array(
          'post_type' => 'post',
          'tax_query' => array(
          array(
          'taxonomy' => 'category',
          'terms' => $term_ids,
          ),
          ),
          );

          $query = new WP_Query( $args );

          if ( $query->have_posts() ) {
          echo '<ul>';
          while ( $query->have_posts() ) {
          $query->the_post();
          echo '<li><a href="' . esc_url( get_the_permalink() ) . '" title="' . get_the_title() . '">' . esc_html( get_the_title() ) . '</a></li>';
          }
          echo '</ul>';
          }
          wp_reset_postdata();

          }





          share|improve this answer














          get_terms does just that, it get the terms for your query. What you'll want is WP_Query with a tax_query. You've already got the categories you want to return the posts for, so it shouldn't be too difficult



          $terms = get_terms( array(
          'taxonomy' => 'category',
          'name__like' => $s,
          'hide_empty' => true // Optional
          ) );

          $term_ids = array();

          if ( ! empty( $terms ) ) {
          foreach( $terms as $term ) {
          $term_ids = $term->term_id;
          }

          $args = array(
          'post_type' => 'post',
          'tax_query' => array(
          array(
          'taxonomy' => 'category',
          'terms' => $term_ids,
          ),
          ),
          );

          $query = new WP_Query( $args );

          if ( $query->have_posts() ) {
          echo '<ul>';
          while ( $query->have_posts() ) {
          $query->the_post();
          echo '<li><a href="' . esc_url( get_the_permalink() ) . '" title="' . get_the_title() . '">' . esc_html( get_the_title() ) . '</a></li>';
          }
          echo '</ul>';
          }
          wp_reset_postdata();

          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 at 2:18

























          answered Nov 15 at 2:03









          cameronjonesweb

          1,64921631




          1,64921631












          • ok let me try this, p.s. syntax error, unexpected '{' on if ( ! empty( $terms ) {
            – rob.m
            Nov 15 at 2:05










          • it is not looking in category, only finds a match of $s in a title, but let's say I search for mario and I have a category called mario bianchi, I need all posts within that category since we found a match of $s (mario) in a category since we have one mario bianchi
            – rob.m
            Nov 15 at 2:07












          • if I do $term_ids = array(); var_dump($terms); i get array(0) {} but we do have a category called mario bianchi
            – rob.m
            Nov 15 at 2:14










          • @rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
            – cameronjonesweb
            Nov 15 at 2:23










          • in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
            – rob.m
            Nov 15 at 2:24




















          • ok let me try this, p.s. syntax error, unexpected '{' on if ( ! empty( $terms ) {
            – rob.m
            Nov 15 at 2:05










          • it is not looking in category, only finds a match of $s in a title, but let's say I search for mario and I have a category called mario bianchi, I need all posts within that category since we found a match of $s (mario) in a category since we have one mario bianchi
            – rob.m
            Nov 15 at 2:07












          • if I do $term_ids = array(); var_dump($terms); i get array(0) {} but we do have a category called mario bianchi
            – rob.m
            Nov 15 at 2:14










          • @rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
            – cameronjonesweb
            Nov 15 at 2:23










          • in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
            – rob.m
            Nov 15 at 2:24


















          ok let me try this, p.s. syntax error, unexpected '{' on if ( ! empty( $terms ) {
          – rob.m
          Nov 15 at 2:05




          ok let me try this, p.s. syntax error, unexpected '{' on if ( ! empty( $terms ) {
          – rob.m
          Nov 15 at 2:05












          it is not looking in category, only finds a match of $s in a title, but let's say I search for mario and I have a category called mario bianchi, I need all posts within that category since we found a match of $s (mario) in a category since we have one mario bianchi
          – rob.m
          Nov 15 at 2:07






          it is not looking in category, only finds a match of $s in a title, but let's say I search for mario and I have a category called mario bianchi, I need all posts within that category since we found a match of $s (mario) in a category since we have one mario bianchi
          – rob.m
          Nov 15 at 2:07














          if I do $term_ids = array(); var_dump($terms); i get array(0) {} but we do have a category called mario bianchi
          – rob.m
          Nov 15 at 2:14




          if I do $term_ids = array(); var_dump($terms); i get array(0) {} but we do have a category called mario bianchi
          – rob.m
          Nov 15 at 2:14












          @rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
          – cameronjonesweb
          Nov 15 at 2:23




          @rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
          – cameronjonesweb
          Nov 15 at 2:23












          in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
          – rob.m
          Nov 15 at 2:24






          in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
          – rob.m
          Nov 15 at 2:24




















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53311303%2fhow-to-search-for-a-string-in-a-category-name%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

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?