getting wp posts by tags similar to arg - wp db query











up vote
1
down vote

favorite












I use below code for getting posts by a word similar tag but not working



$query = "
SELECT *
FROM $wpdb->posts , $wpdb->terms , $wpdb->term_relationships
WHERE $wpdb->terms.name LIKE '$search_query%'
AND $wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id
AND $wpdb->term_relationship.object_id = $wpdb->posts.ID
ORDER BY $wpdb->posts.post_title";


$search_songs = $wpdb->get_results($query);

$new_query = new WP_Query( $search_songs );
$search_songs_posts = $new_query->posts;


Where is wrong?










share|improve this question




























    up vote
    1
    down vote

    favorite












    I use below code for getting posts by a word similar tag but not working



    $query = "
    SELECT *
    FROM $wpdb->posts , $wpdb->terms , $wpdb->term_relationships
    WHERE $wpdb->terms.name LIKE '$search_query%'
    AND $wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id
    AND $wpdb->term_relationship.object_id = $wpdb->posts.ID
    ORDER BY $wpdb->posts.post_title";


    $search_songs = $wpdb->get_results($query);

    $new_query = new WP_Query( $search_songs );
    $search_songs_posts = $new_query->posts;


    Where is wrong?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I use below code for getting posts by a word similar tag but not working



      $query = "
      SELECT *
      FROM $wpdb->posts , $wpdb->terms , $wpdb->term_relationships
      WHERE $wpdb->terms.name LIKE '$search_query%'
      AND $wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id
      AND $wpdb->term_relationship.object_id = $wpdb->posts.ID
      ORDER BY $wpdb->posts.post_title";


      $search_songs = $wpdb->get_results($query);

      $new_query = new WP_Query( $search_songs );
      $search_songs_posts = $new_query->posts;


      Where is wrong?










      share|improve this question















      I use below code for getting posts by a word similar tag but not working



      $query = "
      SELECT *
      FROM $wpdb->posts , $wpdb->terms , $wpdb->term_relationships
      WHERE $wpdb->terms.name LIKE '$search_query%'
      AND $wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id
      AND $wpdb->term_relationship.object_id = $wpdb->posts.ID
      ORDER BY $wpdb->posts.post_title";


      $search_songs = $wpdb->get_results($query);

      $new_query = new WP_Query( $search_songs );
      $search_songs_posts = $new_query->posts;


      Where is wrong?







      mysql sql wordpress search tags






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 7:34









      Gufran Hasan

      3,26831325




      3,26831325










      asked Nov 19 at 7:23









      ebeliejinfren

      156




      156
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          As I have seen your first query looks fine.



          $query_byTag="
          SELECT wp_posts.ID
          FROM wp_posts, wp_term_relationships, wp_terms
          WHERE wp_posts.ID = wp_term_relationships.object_id
          AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
          AND wp_terms.name LIKE '".$search_query."%'";
          $search_songs = $wpdb->get_results($query_byTag);


          This query returns all posts IDs as an array of object.



           Array
          (
          [0] => stdClass Object
          (
          [ID] => 1
          )

          [1] => stdClass Object
          (
          [ID] => 60
          )

          )


          Now you need to pass $search_songs array of object WP_Query($search_songs) as:



          $new_query = new WP_Query( $search_songs );
          echo '<pre>';
          print_r($search_songs);//returns all posts with query and others data
          echo '</pre>';


          If you want to get only posts then you can get it as:



          echo '<pre>';
          print_r($search_songs->posts);//returns all posts
          echo '</pre>';


          For more details






          share|improve this answer























          • i don't understand, what's problem ? why need loop and where exactly ?
            – ebeliejinfren
            Nov 19 at 7:55










          • do one thing print_r($search_songs).
            – Gufran Hasan
            Nov 19 at 7:56










          • i know, but don't return any things
            – ebeliejinfren
            Nov 19 at 11:48










          • Are you getting any error?
            – Gufran Hasan
            Nov 19 at 12:26










          • @ebeliejinfren, I have updated my answer and also tested it, now It's working.
            – Gufran Hasan
            Nov 19 at 12:44











          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%2f53370014%2fgetting-wp-posts-by-tags-similar-to-arg-wp-db-query%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
          1
          down vote



          accepted










          As I have seen your first query looks fine.



          $query_byTag="
          SELECT wp_posts.ID
          FROM wp_posts, wp_term_relationships, wp_terms
          WHERE wp_posts.ID = wp_term_relationships.object_id
          AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
          AND wp_terms.name LIKE '".$search_query."%'";
          $search_songs = $wpdb->get_results($query_byTag);


          This query returns all posts IDs as an array of object.



           Array
          (
          [0] => stdClass Object
          (
          [ID] => 1
          )

          [1] => stdClass Object
          (
          [ID] => 60
          )

          )


          Now you need to pass $search_songs array of object WP_Query($search_songs) as:



          $new_query = new WP_Query( $search_songs );
          echo '<pre>';
          print_r($search_songs);//returns all posts with query and others data
          echo '</pre>';


          If you want to get only posts then you can get it as:



          echo '<pre>';
          print_r($search_songs->posts);//returns all posts
          echo '</pre>';


          For more details






          share|improve this answer























          • i don't understand, what's problem ? why need loop and where exactly ?
            – ebeliejinfren
            Nov 19 at 7:55










          • do one thing print_r($search_songs).
            – Gufran Hasan
            Nov 19 at 7:56










          • i know, but don't return any things
            – ebeliejinfren
            Nov 19 at 11:48










          • Are you getting any error?
            – Gufran Hasan
            Nov 19 at 12:26










          • @ebeliejinfren, I have updated my answer and also tested it, now It's working.
            – Gufran Hasan
            Nov 19 at 12:44















          up vote
          1
          down vote



          accepted










          As I have seen your first query looks fine.



          $query_byTag="
          SELECT wp_posts.ID
          FROM wp_posts, wp_term_relationships, wp_terms
          WHERE wp_posts.ID = wp_term_relationships.object_id
          AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
          AND wp_terms.name LIKE '".$search_query."%'";
          $search_songs = $wpdb->get_results($query_byTag);


          This query returns all posts IDs as an array of object.



           Array
          (
          [0] => stdClass Object
          (
          [ID] => 1
          )

          [1] => stdClass Object
          (
          [ID] => 60
          )

          )


          Now you need to pass $search_songs array of object WP_Query($search_songs) as:



          $new_query = new WP_Query( $search_songs );
          echo '<pre>';
          print_r($search_songs);//returns all posts with query and others data
          echo '</pre>';


          If you want to get only posts then you can get it as:



          echo '<pre>';
          print_r($search_songs->posts);//returns all posts
          echo '</pre>';


          For more details






          share|improve this answer























          • i don't understand, what's problem ? why need loop and where exactly ?
            – ebeliejinfren
            Nov 19 at 7:55










          • do one thing print_r($search_songs).
            – Gufran Hasan
            Nov 19 at 7:56










          • i know, but don't return any things
            – ebeliejinfren
            Nov 19 at 11:48










          • Are you getting any error?
            – Gufran Hasan
            Nov 19 at 12:26










          • @ebeliejinfren, I have updated my answer and also tested it, now It's working.
            – Gufran Hasan
            Nov 19 at 12:44













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          As I have seen your first query looks fine.



          $query_byTag="
          SELECT wp_posts.ID
          FROM wp_posts, wp_term_relationships, wp_terms
          WHERE wp_posts.ID = wp_term_relationships.object_id
          AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
          AND wp_terms.name LIKE '".$search_query."%'";
          $search_songs = $wpdb->get_results($query_byTag);


          This query returns all posts IDs as an array of object.



           Array
          (
          [0] => stdClass Object
          (
          [ID] => 1
          )

          [1] => stdClass Object
          (
          [ID] => 60
          )

          )


          Now you need to pass $search_songs array of object WP_Query($search_songs) as:



          $new_query = new WP_Query( $search_songs );
          echo '<pre>';
          print_r($search_songs);//returns all posts with query and others data
          echo '</pre>';


          If you want to get only posts then you can get it as:



          echo '<pre>';
          print_r($search_songs->posts);//returns all posts
          echo '</pre>';


          For more details






          share|improve this answer














          As I have seen your first query looks fine.



          $query_byTag="
          SELECT wp_posts.ID
          FROM wp_posts, wp_term_relationships, wp_terms
          WHERE wp_posts.ID = wp_term_relationships.object_id
          AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
          AND wp_terms.name LIKE '".$search_query."%'";
          $search_songs = $wpdb->get_results($query_byTag);


          This query returns all posts IDs as an array of object.



           Array
          (
          [0] => stdClass Object
          (
          [ID] => 1
          )

          [1] => stdClass Object
          (
          [ID] => 60
          )

          )


          Now you need to pass $search_songs array of object WP_Query($search_songs) as:



          $new_query = new WP_Query( $search_songs );
          echo '<pre>';
          print_r($search_songs);//returns all posts with query and others data
          echo '</pre>';


          If you want to get only posts then you can get it as:



          echo '<pre>';
          print_r($search_songs->posts);//returns all posts
          echo '</pre>';


          For more details







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 12:45

























          answered Nov 19 at 7:46









          Gufran Hasan

          3,26831325




          3,26831325












          • i don't understand, what's problem ? why need loop and where exactly ?
            – ebeliejinfren
            Nov 19 at 7:55










          • do one thing print_r($search_songs).
            – Gufran Hasan
            Nov 19 at 7:56










          • i know, but don't return any things
            – ebeliejinfren
            Nov 19 at 11:48










          • Are you getting any error?
            – Gufran Hasan
            Nov 19 at 12:26










          • @ebeliejinfren, I have updated my answer and also tested it, now It's working.
            – Gufran Hasan
            Nov 19 at 12:44


















          • i don't understand, what's problem ? why need loop and where exactly ?
            – ebeliejinfren
            Nov 19 at 7:55










          • do one thing print_r($search_songs).
            – Gufran Hasan
            Nov 19 at 7:56










          • i know, but don't return any things
            – ebeliejinfren
            Nov 19 at 11:48










          • Are you getting any error?
            – Gufran Hasan
            Nov 19 at 12:26










          • @ebeliejinfren, I have updated my answer and also tested it, now It's working.
            – Gufran Hasan
            Nov 19 at 12:44
















          i don't understand, what's problem ? why need loop and where exactly ?
          – ebeliejinfren
          Nov 19 at 7:55




          i don't understand, what's problem ? why need loop and where exactly ?
          – ebeliejinfren
          Nov 19 at 7:55












          do one thing print_r($search_songs).
          – Gufran Hasan
          Nov 19 at 7:56




          do one thing print_r($search_songs).
          – Gufran Hasan
          Nov 19 at 7:56












          i know, but don't return any things
          – ebeliejinfren
          Nov 19 at 11:48




          i know, but don't return any things
          – ebeliejinfren
          Nov 19 at 11:48












          Are you getting any error?
          – Gufran Hasan
          Nov 19 at 12:26




          Are you getting any error?
          – Gufran Hasan
          Nov 19 at 12:26












          @ebeliejinfren, I have updated my answer and also tested it, now It's working.
          – Gufran Hasan
          Nov 19 at 12:44




          @ebeliejinfren, I have updated my answer and also tested it, now It's working.
          – Gufran Hasan
          Nov 19 at 12:44


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370014%2fgetting-wp-posts-by-tags-similar-to-arg-wp-db-query%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin