How to consolidate 2 database calls - wordpress

I'm not experienced with WordPress so please excuse me if the question is stupid.
I have a custom template page that displays record information obtained from database. This page also call get_header() to generate the header part of the page.
I want that the page title (in HEAD HTML) will display some of the record information also.
What is the best way to do this, the "WordPress way", with 1 database query?
Currently I execute 2 identical database queries: one in add_filter('wp_title', myfunc) # function.php and the other in my custom template PHP. This duplication looks wrong.

OK, figured that out... I guess I have to use global wordpress database connection ($wpdb)

Related

Wordpress: How to create frontend table using data from backend DB table

I am relatively new to WP, and am trying to figure out if the following can be done, or if a plugin exists?
I am looking to save data (500K+ rows) into a SQL table (maybe a new table in WP database), and then render the results on a page (frontend) using some basic JS and CSS. Because of the size of data, I am not sure if I should use a plugin that imports from CSV/JSON and would it slow down the site.
Do I have to go all the way and create models and views and then render to an HTML, or is there an easy hack available?
As the last resort, I am also thinking of creating the table part using Flask and SQlite and use it as main domain, and WP blog as subdomain (don't know if it will impact SEO).
Request guidance from experts. TIA
There are plugins to handle table work. Use your favorite internet search engine to find them. You'll have to experiment with various plugins to get one that meets your needs if you go that route.
There two questions here.
One is how to render a table on the front end. If you do this yourself , your best bet is to create a plugin that defines a shortcode. When you write php code for that shortcode's handler, you will include code to read and render the table. You'll probably render it as an ordinary html <table>.
Then, you'll use WordPress's content editor to create a post, or a page, that contains your shortcode. When a visitor views the post or page, your shortcode handler runs and delivers the table you want.
The WordPress plugin API also gives you a way to enqueue css and Javascript to your pages. And, jquery is an inherent part of WordPress's page loads. WordPress also contains most of jqueryUI. That's handy, because there's a really nice jquery-based plugin called Datatables that will get your users some convenient features like pagination and sorting.
The second question is how to load that data into a table. The easy way would be to do it outside WordPress, with a SQL file or some other program to bulk-load it. But if you do it inside WordPress, you'll do it from an administrative page.
If you need to be able to add, update, or delete table entries from the front end, that's going to be some work.
Explaining all this in detail is far outside the scope of a Stack Overflow answer.

Drupal 8 getting data from database

I have a task of making some content of movies in admin panel in drupal
https://imgur.com/a/5eJp9ZQ
and then after that to use a custom module and controller to show those movies onto my custom twig.
I am unable to find a solution or hint how to do that, maybe i even did find something but because of my small knowledge of drupal i dont know what i have to do in order to achieve that.
I created a custom module, controller , routing and everything while following the hello world part of drupal documentation but after that i just frooze and i am unable to continue further
Can someone assist me on this task, will be greatly appreciated because i have been stuck on it for 2 days now
Drupal's way of getting content is by using views. You should create a view (Structure -> Views -> Add view from admin menu) to get that data. It's something like visual interface for creating database queries, and it's not hard as it might look at first sight.
Then, there are few way how to use that view.
View can create a page, or a block, so content will appear on the page
You can execute view from your controller and collect data view returned and pass it to twig template to render the data.
You could embed view directly from twig, so no need for any kind of coding inside page controller.
Option 1 with "page view" (it's display type is "page") is probably easiest one, but doesn't fulfill request of custom controller.
With option 2 you could use: views_get_view_result() call to get results:
https://api.drupal.org/api/drupal/core%21modules%21views%21views.module/function/views_get_view_result/8.2.x
It should look something like:
$view = Views::getView('view_machine_name');
$view->execute();
foreach ($view->result as $row) { // iterating trough rows of results
// Do something with $row
}
This option matches the best your request.
For option 3 you would have to Twig Tweak module to embed view from twig:
https://www.drupal.org/docs/8/modules/twig-tweak/twig-tweak-and-views
Beside that you could of course create custom database query, but that's not so "drupalish" way and queries can be pretty complicated since drupal's database structure is not the simplest one and you could get lost.

create a post in Wordpress with an external script

I am making a java script to create a few posts in a Wordpress blog by only inserting them into the wp_posts so table. So far I could browse the posts on web browser. Question is, is it good enough with the db operations ? does anyone know any other subsequent tables need to be updated for making a post ?
Not really, you can extend the default and existing WordPress posts.
But you can go ahead and create your own table like: https://github.com/wp-plugins/wordpress-guest-post/blob/master/wordpressguestpost.php
ended up adding REST API V2 plugin and make calls to it for creating all sorts of things. Worked like a champ

Wordpress loop with pages

Im pretty new to playing around with what the loop can do and i have been looking for an answer for the last few days. So I have 2 pages services-page.php and contact-page.php both with custom templates. What i would like to know is it possible to set up the loop in page.php to get those custom templates with get_template_part() so when someone goes to the services page it goes to it and same with the contact page. So instead of having a loop on every different page I can just have it in page.php and bring in my different templates which goes to different pages. Hope that was clear, any help would be appreciated.
You are almost there with the get_template_part() but instead you would create a template part for your loop and include that on your templates. So create a php "part" that contains your loop and any other code you need.https://codex.wordpress.org/Function_Reference/get_template_part has an example with "loop.php" but you could create "loop-name.php" (where "name" can be anything you like) for your own loop and use this.

WordPress Plugin Development Idea? Is this possible? Am I on the right track?

I'm very new to WP development. I host a website which needs a list of trails (hiking, biking, etc) and I'd like to write a WordPress plugin to do it.
Can someone please tell me if I'm taking the right approach, and if what I'm proposing is possible.
I'd like the site to end up with an auto-generated and filtered index at http://example.com/trail-guide, and the discrete trail info pages at http://example.com/trail-guide/trailname. This data would all be stored in a single database table holding info for each trail, with an admin page for adding, editing, and deleting entries from here.
Is a WP plugin the best way to go about doing this, or should I be looking at something else?
From the way you're describing, your best bet would be to Register A Custom Post Type. This can be done by adding to your existing theme's Functions.php file, or by creating a plugin.
If you don't plan on changing themes, my advice would be to just hardcode everything into your functions.php file. Otherwise, creating a plugin for this particular job would be the safest alternative.
Using this functionality in tandem with Custom Meta Boxes and Custom Taxonomies will allow you to keep everything organized within the Wordpress Framework with your own special data.
This means that these new posts can also be queried at any time through the standard Wordpress Loop or search box.
If you are uncomfortable with writing your own functions to extend your existing framework, you might want to look into some plugins like GD Custom Posts And Taxonomies Tools to manage your own.
Hope this helps.

Resources