Create Pages from Database





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















i've recently decided to create a website with wordpress.



My goal is to a visualize data stored in a MySQL database. Each id in the database should represent one page on the website. From what i've read a custom post type or a custom page template should do the trick.



But my biggest problem is that pretty much all tutorials i've read or watched so far explain how to create a page for each "product" manually. My database currently has ~44.000 entries and there are more to come on a weekly basis, so this can't be the right way.



Is there some way to dynamically create a page based of a template after the user searched for it?










share|improve this question







New contributor




QUE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



























    1















    i've recently decided to create a website with wordpress.



    My goal is to a visualize data stored in a MySQL database. Each id in the database should represent one page on the website. From what i've read a custom post type or a custom page template should do the trick.



    But my biggest problem is that pretty much all tutorials i've read or watched so far explain how to create a page for each "product" manually. My database currently has ~44.000 entries and there are more to come on a weekly basis, so this can't be the right way.



    Is there some way to dynamically create a page based of a template after the user searched for it?










    share|improve this question







    New contributor




    QUE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1












      1








      1


      1






      i've recently decided to create a website with wordpress.



      My goal is to a visualize data stored in a MySQL database. Each id in the database should represent one page on the website. From what i've read a custom post type or a custom page template should do the trick.



      But my biggest problem is that pretty much all tutorials i've read or watched so far explain how to create a page for each "product" manually. My database currently has ~44.000 entries and there are more to come on a weekly basis, so this can't be the right way.



      Is there some way to dynamically create a page based of a template after the user searched for it?










      share|improve this question







      New contributor




      QUE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      i've recently decided to create a website with wordpress.



      My goal is to a visualize data stored in a MySQL database. Each id in the database should represent one page on the website. From what i've read a custom post type or a custom page template should do the trick.



      But my biggest problem is that pretty much all tutorials i've read or watched so far explain how to create a page for each "product" manually. My database currently has ~44.000 entries and there are more to come on a weekly basis, so this can't be the right way.



      Is there some way to dynamically create a page based of a template after the user searched for it?







      custom-post-types database templates mysql






      share|improve this question







      New contributor




      QUE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      QUE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      QUE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      QUEQUE

      61




      61




      New contributor




      QUE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      QUE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      QUE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          2














          There seems little point in creating a new post entry for each of your products if your table of products already has all of the information you wish to display.



          Your best bet is to create a single page for example "Product" and then setup your own rewrite rule, using add_rewrite_rule, so that you can setup links such as /product/some-product-alias. You would then need to perform a query on the product alias to lookup the product information from your product table and then display this on the template (either use page-product.php or create a template and assign this in the admin)



          There's a little more information on setting up the redirects in this answer of mine.






          share|improve this answer































            1














            You could add a custom template when certain URL patterns are matched and then print on it whatever you want using your own functions to return your database query results dynamically.



            The following sample adds a new custom template suggestion. So that always when the URL contains foobar as first segment and a non-empty second segment the foobar.php template from your current theme will be rendered.



            add_action('template_include', 'template_suggestions');
            function template_suggestions($template) {

            $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
            $foobar_template = locate_template(['foobar.php']);

            if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar' && $foobar_template !== '') {

            status_header(200);
            return $foobar_template;
            }

            return $template;
            }


            Now use any other custom function to return your database query results.



            function foobar_results() {

            $myrows = ;
            $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

            if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar') {

            global $wpdb;
            $myrows = $wpdb->get_results( "SELECT * FROM mytable WHERE id = {$segments[2]}" );
            }

            return $myrows;
            }


            And then in foobar.php simply loop through the results.



            <?php foreach(foobar_results() as $row): ?>
            <?php print $row->id; ?>
            <?php endforeach; ?>





            share|improve this answer
























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "110"
              };
              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: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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
              });


              }
              });






              QUE is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f334410%2fcreate-pages-from-database%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









              2














              There seems little point in creating a new post entry for each of your products if your table of products already has all of the information you wish to display.



              Your best bet is to create a single page for example "Product" and then setup your own rewrite rule, using add_rewrite_rule, so that you can setup links such as /product/some-product-alias. You would then need to perform a query on the product alias to lookup the product information from your product table and then display this on the template (either use page-product.php or create a template and assign this in the admin)



              There's a little more information on setting up the redirects in this answer of mine.






              share|improve this answer




























                2














                There seems little point in creating a new post entry for each of your products if your table of products already has all of the information you wish to display.



                Your best bet is to create a single page for example "Product" and then setup your own rewrite rule, using add_rewrite_rule, so that you can setup links such as /product/some-product-alias. You would then need to perform a query on the product alias to lookup the product information from your product table and then display this on the template (either use page-product.php or create a template and assign this in the admin)



                There's a little more information on setting up the redirects in this answer of mine.






                share|improve this answer


























                  2












                  2








                  2







                  There seems little point in creating a new post entry for each of your products if your table of products already has all of the information you wish to display.



                  Your best bet is to create a single page for example "Product" and then setup your own rewrite rule, using add_rewrite_rule, so that you can setup links such as /product/some-product-alias. You would then need to perform a query on the product alias to lookup the product information from your product table and then display this on the template (either use page-product.php or create a template and assign this in the admin)



                  There's a little more information on setting up the redirects in this answer of mine.






                  share|improve this answer













                  There seems little point in creating a new post entry for each of your products if your table of products already has all of the information you wish to display.



                  Your best bet is to create a single page for example "Product" and then setup your own rewrite rule, using add_rewrite_rule, so that you can setup links such as /product/some-product-alias. You would then need to perform a query on the product alias to lookup the product information from your product table and then display this on the template (either use page-product.php or create a template and assign this in the admin)



                  There's a little more information on setting up the redirects in this answer of mine.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  Alexander HolsgroveAlexander Holsgrove

                  1,1531916




                  1,1531916

























                      1














                      You could add a custom template when certain URL patterns are matched and then print on it whatever you want using your own functions to return your database query results dynamically.



                      The following sample adds a new custom template suggestion. So that always when the URL contains foobar as first segment and a non-empty second segment the foobar.php template from your current theme will be rendered.



                      add_action('template_include', 'template_suggestions');
                      function template_suggestions($template) {

                      $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
                      $foobar_template = locate_template(['foobar.php']);

                      if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar' && $foobar_template !== '') {

                      status_header(200);
                      return $foobar_template;
                      }

                      return $template;
                      }


                      Now use any other custom function to return your database query results.



                      function foobar_results() {

                      $myrows = ;
                      $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

                      if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar') {

                      global $wpdb;
                      $myrows = $wpdb->get_results( "SELECT * FROM mytable WHERE id = {$segments[2]}" );
                      }

                      return $myrows;
                      }


                      And then in foobar.php simply loop through the results.



                      <?php foreach(foobar_results() as $row): ?>
                      <?php print $row->id; ?>
                      <?php endforeach; ?>





                      share|improve this answer




























                        1














                        You could add a custom template when certain URL patterns are matched and then print on it whatever you want using your own functions to return your database query results dynamically.



                        The following sample adds a new custom template suggestion. So that always when the URL contains foobar as first segment and a non-empty second segment the foobar.php template from your current theme will be rendered.



                        add_action('template_include', 'template_suggestions');
                        function template_suggestions($template) {

                        $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
                        $foobar_template = locate_template(['foobar.php']);

                        if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar' && $foobar_template !== '') {

                        status_header(200);
                        return $foobar_template;
                        }

                        return $template;
                        }


                        Now use any other custom function to return your database query results.



                        function foobar_results() {

                        $myrows = ;
                        $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

                        if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar') {

                        global $wpdb;
                        $myrows = $wpdb->get_results( "SELECT * FROM mytable WHERE id = {$segments[2]}" );
                        }

                        return $myrows;
                        }


                        And then in foobar.php simply loop through the results.



                        <?php foreach(foobar_results() as $row): ?>
                        <?php print $row->id; ?>
                        <?php endforeach; ?>





                        share|improve this answer


























                          1












                          1








                          1







                          You could add a custom template when certain URL patterns are matched and then print on it whatever you want using your own functions to return your database query results dynamically.



                          The following sample adds a new custom template suggestion. So that always when the URL contains foobar as first segment and a non-empty second segment the foobar.php template from your current theme will be rendered.



                          add_action('template_include', 'template_suggestions');
                          function template_suggestions($template) {

                          $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
                          $foobar_template = locate_template(['foobar.php']);

                          if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar' && $foobar_template !== '') {

                          status_header(200);
                          return $foobar_template;
                          }

                          return $template;
                          }


                          Now use any other custom function to return your database query results.



                          function foobar_results() {

                          $myrows = ;
                          $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

                          if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar') {

                          global $wpdb;
                          $myrows = $wpdb->get_results( "SELECT * FROM mytable WHERE id = {$segments[2]}" );
                          }

                          return $myrows;
                          }


                          And then in foobar.php simply loop through the results.



                          <?php foreach(foobar_results() as $row): ?>
                          <?php print $row->id; ?>
                          <?php endforeach; ?>





                          share|improve this answer













                          You could add a custom template when certain URL patterns are matched and then print on it whatever you want using your own functions to return your database query results dynamically.



                          The following sample adds a new custom template suggestion. So that always when the URL contains foobar as first segment and a non-empty second segment the foobar.php template from your current theme will be rendered.



                          add_action('template_include', 'template_suggestions');
                          function template_suggestions($template) {

                          $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
                          $foobar_template = locate_template(['foobar.php']);

                          if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar' && $foobar_template !== '') {

                          status_header(200);
                          return $foobar_template;
                          }

                          return $template;
                          }


                          Now use any other custom function to return your database query results.



                          function foobar_results() {

                          $myrows = ;
                          $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

                          if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar') {

                          global $wpdb;
                          $myrows = $wpdb->get_results( "SELECT * FROM mytable WHERE id = {$segments[2]}" );
                          }

                          return $myrows;
                          }


                          And then in foobar.php simply loop through the results.



                          <?php foreach(foobar_results() as $row): ?>
                          <?php print $row->id; ?>
                          <?php endforeach; ?>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered yesterday









                          leymannxleymannx

                          95411122




                          95411122






















                              QUE is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              QUE is a new contributor. Be nice, and check out our Code of Conduct.













                              QUE is a new contributor. Be nice, and check out our Code of Conduct.












                              QUE is a new contributor. Be nice, and check out our Code of Conduct.
















                              Thanks for contributing an answer to WordPress Development Stack Exchange!


                              • 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%2fwordpress.stackexchange.com%2fquestions%2f334410%2fcreate-pages-from-database%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

                              How did Captain America manage to do this?

                              迪纳利

                              南乌拉尔铁路局