Sort by Meta Value with mixing Custom Post Type in WordPress












0















I created Custom Post Type in WordPress to record certain data like product id of woocommerce and some personal user entered information in frontend, and those information are properly listed in wp list table, now i am trying to sort by product name, but i have only product id,(in list table i fetch product name using woocommerce native function), sort by product id was working properly using the below code



add_filter('parse_query', array($this, 'parse_query'));
public function parse_query($query) {
global $pagenow;
if (!is_admin())
return;

$orderby = $query->get('orderby');
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
//frontendtestwp is a custom cpt of wordpress
if ('frontendtestwp' == $type && is_admin() && $pagenow == 'edit.php') {
// for orderby just order based on product id

$query->set('meta_key', 'frontend_pid'); //contain product id
$query->set('orderby', 'meta_value_num');
}
}
}


Now if i want to sort based on product name i have two option one is to record the product name in my side additionally(which i don't want to do, if any update to the original product name then that sorting result will be based on old data).



Second option is using posts_where filter(currently trying this option) as my idea is to check custom post type of product(woocommerce) additionally with product_id = my meta value and order by product title



in posts_where filter i added the below code to check in product as well but i am stuck in condition check with (id of product==my_meta_value_product_id)



$where .= " AND `post_type`='product'";


Initially i thought to use Inner join but it is not needed because all custom post types are in one table, only thing i want to check is order by title of product based on my meta product id.



Is there any way to achieve my desired result with my existing data(product id)?










share|improve this question



























    0















    I created Custom Post Type in WordPress to record certain data like product id of woocommerce and some personal user entered information in frontend, and those information are properly listed in wp list table, now i am trying to sort by product name, but i have only product id,(in list table i fetch product name using woocommerce native function), sort by product id was working properly using the below code



    add_filter('parse_query', array($this, 'parse_query'));
    public function parse_query($query) {
    global $pagenow;
    if (!is_admin())
    return;

    $orderby = $query->get('orderby');
    if (isset($_GET['post_type'])) {
    $type = $_GET['post_type'];
    //frontendtestwp is a custom cpt of wordpress
    if ('frontendtestwp' == $type && is_admin() && $pagenow == 'edit.php') {
    // for orderby just order based on product id

    $query->set('meta_key', 'frontend_pid'); //contain product id
    $query->set('orderby', 'meta_value_num');
    }
    }
    }


    Now if i want to sort based on product name i have two option one is to record the product name in my side additionally(which i don't want to do, if any update to the original product name then that sorting result will be based on old data).



    Second option is using posts_where filter(currently trying this option) as my idea is to check custom post type of product(woocommerce) additionally with product_id = my meta value and order by product title



    in posts_where filter i added the below code to check in product as well but i am stuck in condition check with (id of product==my_meta_value_product_id)



    $where .= " AND `post_type`='product'";


    Initially i thought to use Inner join but it is not needed because all custom post types are in one table, only thing i want to check is order by title of product based on my meta product id.



    Is there any way to achieve my desired result with my existing data(product id)?










    share|improve this question

























      0












      0








      0








      I created Custom Post Type in WordPress to record certain data like product id of woocommerce and some personal user entered information in frontend, and those information are properly listed in wp list table, now i am trying to sort by product name, but i have only product id,(in list table i fetch product name using woocommerce native function), sort by product id was working properly using the below code



      add_filter('parse_query', array($this, 'parse_query'));
      public function parse_query($query) {
      global $pagenow;
      if (!is_admin())
      return;

      $orderby = $query->get('orderby');
      if (isset($_GET['post_type'])) {
      $type = $_GET['post_type'];
      //frontendtestwp is a custom cpt of wordpress
      if ('frontendtestwp' == $type && is_admin() && $pagenow == 'edit.php') {
      // for orderby just order based on product id

      $query->set('meta_key', 'frontend_pid'); //contain product id
      $query->set('orderby', 'meta_value_num');
      }
      }
      }


      Now if i want to sort based on product name i have two option one is to record the product name in my side additionally(which i don't want to do, if any update to the original product name then that sorting result will be based on old data).



      Second option is using posts_where filter(currently trying this option) as my idea is to check custom post type of product(woocommerce) additionally with product_id = my meta value and order by product title



      in posts_where filter i added the below code to check in product as well but i am stuck in condition check with (id of product==my_meta_value_product_id)



      $where .= " AND `post_type`='product'";


      Initially i thought to use Inner join but it is not needed because all custom post types are in one table, only thing i want to check is order by title of product based on my meta product id.



      Is there any way to achieve my desired result with my existing data(product id)?










      share|improve this question














      I created Custom Post Type in WordPress to record certain data like product id of woocommerce and some personal user entered information in frontend, and those information are properly listed in wp list table, now i am trying to sort by product name, but i have only product id,(in list table i fetch product name using woocommerce native function), sort by product id was working properly using the below code



      add_filter('parse_query', array($this, 'parse_query'));
      public function parse_query($query) {
      global $pagenow;
      if (!is_admin())
      return;

      $orderby = $query->get('orderby');
      if (isset($_GET['post_type'])) {
      $type = $_GET['post_type'];
      //frontendtestwp is a custom cpt of wordpress
      if ('frontendtestwp' == $type && is_admin() && $pagenow == 'edit.php') {
      // for orderby just order based on product id

      $query->set('meta_key', 'frontend_pid'); //contain product id
      $query->set('orderby', 'meta_value_num');
      }
      }
      }


      Now if i want to sort based on product name i have two option one is to record the product name in my side additionally(which i don't want to do, if any update to the original product name then that sorting result will be based on old data).



      Second option is using posts_where filter(currently trying this option) as my idea is to check custom post type of product(woocommerce) additionally with product_id = my meta value and order by product title



      in posts_where filter i added the below code to check in product as well but i am stuck in condition check with (id of product==my_meta_value_product_id)



      $where .= " AND `post_type`='product'";


      Initially i thought to use Inner join but it is not needed because all custom post types are in one table, only thing i want to check is order by title of product based on my meta product id.



      Is there any way to achieve my desired result with my existing data(product id)?







      php wordpress woocommerce






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 8:17









      Vignesh PichamaniVignesh Pichamani

      3,971195697




      3,971195697
























          0






          active

          oldest

          votes











          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%2f53426521%2fsort-by-meta-value-with-mixing-custom-post-type-in-wordpress%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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%2f53426521%2fsort-by-meta-value-with-mixing-custom-post-type-in-wordpress%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