I am relatively new to WordPress but have a lot of coding experience so I am hoping to pick it up quite quickly.
I am looking to create a website on WordPress that uses a database.
For example, I have made a table of animals consisting of 100 rows, one for each animal, and this is accessible on phpMyAdmin. I want my website to have a page for each row entry in this database.
Ideally, I would like a page to be shown to the user which contains filters. So the user can select maybe "Small" animals which are "Nocturnal". This will then use the database to find all animals which are "Small" and "Nocturnal", and then the page will display the links to the animals which match these filters. Say only "Hamster" matches these filters in my database, then the link to the Hamster page would display on the website.
Then the user can click the Hamster page to find out more about the animal. So I need a page for each animal, but wondered if I could somehow link this to a database I have to help with the filtering options.
Thanks.
Step 1: Create the animals page and display all of them
Set up a page template to show all your animals first, without filters. That should get you started.
To retrieve them, modify the page template file you created above to fetch all the rows in the animals table, like so:
global $wpdb;
$feeds = $wpdb->get_results("SELECT * FROM animals", ARRAY_A);
Iterate through the rows to print them out into table form.
Step 2: Create the filters
Create the links on the front end using query parameters, e.g. for a link to all nocturnal animals, use something like:
echo 'Nocturnal';
Step 3: Handle a specific query variable
At the top of your animals template page, use the following code to get a query variable:
$animal_type = get_query_var('animal_type');
So if you accessed the URL http://www.example.com/animals/?animal_type=nocturnal then the value of $animal_type would now be nocturnal.
Now create a new function in functions.php to query for animals using query parameters. Something like this:
$args = array('animal_type' => $animal_type, 'animal_size' => $animal_size);
function get_animals($args);
And iterate through them to print them out.
Related
Update: With the help of the answerer below I figured out how to do this. Basically I used a WP Query to get users and their user metadata and I sorted and placed the data in a for each loop in a table.
I'm new to PHP and I need some help. Basically I want to create a leaderboard with different user data. I want to display it in an html table.
This is kinda what i want:
Username GamiPress Points Time since last login
And I want the table to be populated with these data amongst others automatically. I want two versions one that is sorted with the 15 users who have the most points and one that sorts on the 15 users that logged in last.
Can someone point me to the right place on how I can best implement this?
I basically want to create the GamiPress Leaderboard add-on that I, unfortunately, can't afford, but with some extra fields.
There are multiple steps to do if you want to achieve this:
1) Add meta field to your users, so you can store the points. For example you can use "Advanced Custom Fields" plugin for this.
2) Write a function for adding points to this field. Define when this function will be fired.
3) Query the users ordered by that meta value and display it (get_users($args) might be useful).
4) For the Last-Login value you can use a plugin (google Wordpress Last Login) and write another Query and order results by that meta field. You can also write this by your own, here is a link I found: https://www.wpbeginner.com/plugins/how-to-show-users-last-login-date-in-wordpress/
I don't know if this is what you were looking for.
Or did you want to see an example code how you use a wp query and display data in html table?
I have a view showing all the OG groups. Now, I'd like to display a number of members and posts within the group next to each group name. I have spent last few days looking for a solution for my problem, unfortunately with no luck.
This would look like this:
Group name -
Members: X Posts: Y
I tried to create separate views showing list of groups (this was easy) and number of members of each group (easy) and then use views_field_view module to display the number of members as a field under the group name. This almost worked. The problem is that each group shows now a multiple times on the list.
I will be rally grateful for any direction or advice.
Cheers!
The easiest thing to try with your current setup is under "Advanced" > "Query settings" select distinct on the first view.
If this dosnt work, instead of using views_field_view just output the group ID in the first view and then theme this field in a views template file to bring in your other view setting the group id as the argument to this view.
Here is an example, put the following code in a file called:
views-view-field--view1name--groupidfieldname.tpl.php
changing the groupid, view and display names.
<?php
$view = views_get_view('view_2_name');
$view->set_display('display_name');
$view->set_arguments(array( $row->node_field_data_field_groupidfieldname ));
$view->execute();
print $view_leader->render();
?>
I think your answer is here: https://drupal.org/node/1101102#comment-7404856
You could also use https://drupal.org/project/views_php .There you can access the group id and query the members count and many other funny things.
I need to create a block with eg the latest comments on the site.
when using views, concegui select the data I wanted, but the problem is that I need to edit the output of view (specify the html). I tried to make a tpl, but the fields when they get to this, are already formatted ([#markup]). also tried to make a block programmatically by accessing the fields of view, via $comments = views_get_view('last_opinions');, but so the fields do not bring content, but for example, ids (for referrals), or integers (in the case of dates), ....
basically, how to change views output?
views->your view->advance(third panel)-> Theme: Information -> click Information ->choose tpl as per your requirement and find $row which actually prints your output
I am using facebook graph Api for my application where i want the data of this page.
http://www.facebook.com/pages/
In this page there is option TV SHOW I want collect all information of that page.
But i didn't any graph api method for this page.
Please help me for this problem.
As far as I can tell, this isn't something you can do. It has to do with what Facebook sets as "indexable" columns in their tables. For example, the page table has a type ID and the TV SHOWS category ID appears to be 89, so you'd think you could run a FQL query such as:
https://api.facebook.com/method/fql.query?query=SELECT page_id, name FROM page WHERE type=89
or perhaps:
https://api.facebook.com/method/fql.query?query=SELECT page_id, name FROM page WHERE type='TV SHOW'
But you'll get this notice:
Your statement is not indexable. The
WHERE clause must contain an indexable
column.
Unfortunately, the only two indexable columns for WHERE statements using the page table are page_id and name. So you can only query this table for one page at a time. Which makes sense when you consider how expensive a search like that would be on billions of records.
You can do a generic graph search for "TV SHOWS" but it won't give the the results on the page index as you requested:
https://graph.facebook.com/search?q=TV%20SHOWS&type=page
The Graph API is more about introspection/connections between objects than an open search protocol.
I have a page which should list nodes. The views is called from a locality page (a taxonomy term page). What I need is almost the same as using the Taxonomy: tid in arguments and passing the tid.
I can't use the term_node table, as (for other reasons) we have a custom table term_node_hierarchy (with nid and tid only). The table term_node_hierarchy is like term_node but also saves the tid of the parents (from an "external" code)
I've been looking for options but still no joy.
Currently I'm building an array of the nid's that should be displayed on the current page, and passing them like print views_build_view('embed', $view, $matching_nids); but the Argument Node: ID states This argument is a single Node ID. As said, only the first node is displayed when printing the views. It would be great if it could filter on more than one nid.
I'm open to any kind of suggestions on how to do this.
Thanks
You could create your own module for this. You could populate the $page_content variable with the results of your own custom query where you allow the user to sort against multiple nids. You could do this a number of different ways. You could display a list of the existing nids with corresponding checkboxes, so that, when the user clicks submit, all the nids that match the selected checkboxes get used in the query. Then you just display the result of that query. That's the easiest way I can think of to offer that degree of flexibility.