• Fat Tony

    (@wildpluckings)


    Hello,

    I have a requirement to build a function on my website that can look at a pool of data and return random results.

    Example; the data set is a list of songs, it shows one at a time with:

    1) a button to select a longer song
    2) a button to select a shorter song
    3) a button to select a heavier song

    etc…

    So if the selected track is 2 minutes, selecting “longer” will randomly select a song >2 minutes from the data set.

    I believe that this functionality is fairly basic. There may be a plugin solution which achieves the same already. Although, I have done hours of google searches and YouTube searches and now I’m asking directly because I’m trying to get this on the right path.

    Any suggestions will be appreciated.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    If the song has a related attachment post which has meta data like its length saved, I’d expect one of the advanced search plugins to be able to do searches like that, but I have no direct experience with them so maybe my expectations are misguided. If my assumptions about your songs are true, you can get a random attachment post meeting some criteria like the length meta value being > 2 min or similar.

    If the data is not as I assumed but resides in the DB somewhere, you could construct a custom MySQL query to do the job. Execute it with one of the global $wpdb object’s methods.

    If the data is not even in the DB, such as an attribute in the audio file itself, you ought to find a way to get the needed data into the DB somehow. Searching through the files themselves is very time consuming.

    Thread Starter Fat Tony

    (@wildpluckings)

    Hi bcworkz.

    I am very grateful for your response.

    I have tried several plugins, but it is rather trial and error and I’m unsure if long term, it’s the way I ought to approach the problem.

    I have little experience with MySQL but I have a high aptitude to learn once I am pointed in the right direction.

    I’ve never written MySQL queries but I understand the concept behind how they work and how relational databases are used. I’ve also never wrote an executable or understand what is meant by “global $wpdb object methods”… although I can interpret from context this is a standard WP command for interacting with the DB from a wordpress page.

    For a quick up-to-speed: is there a tutorial that you could recommend that will have me add a line of code to an empty WordPress page that calls data from the DB?
    …from that very primitive position; I think I’ll be on my journey to victory.

    Moderator bcworkz

    (@bcworkz)

    Not specifically a single tutorial that walks you through a task, but I can give you references to related elements which hopefully you can string together to accomplish your goal. It may be more challenging than a walk through, but I think you’ll also learn more in the process.

    Part of the solution depend on what sort of data you need from the DB. To get WP posts of some sort, such as a media attachment, you can use the WP_Query class. You should find the examples and user notes near the end helpful.

    If you need data from a non-WP table, but within the WP database, you would use that global $wpdb object I mentioned, whose purpose you grasped exactly. The global variable $wpdb contains an instance a wpdb class object. The global object has already connected to the DB when WP was initialized. You can use its class methods to make various DB queries. wpdb::get_var() is a pretty useful method. Or use wpdb::query() to execute any MySQL query you may wish to execute.

    One way to execute PHP code and have results appear on a page is to create a custom page template to contain your code. You might prefer the subsequent section on templates for one specific page.

    You should look over the entire theme handbook into which I’ve linked just to learn what other information is available, some of it will be useful at one point or another. It may not be necessary for this particular task, but also look over the Plugin Handbook to see the sort of information offered.

    You shouldn’t save your template with a distributed theme, it’ll be lost when the theme updates, you should create a child theme to contain your custom work.

    Finally, developing on a hosted site is kind of a pain. Most developers prefer to work on a localhost installation. Being saved from the cycle of upload/download of files may not seem like a much of an advantage, but being saved from doing so dozens of times starts to add up.

    Thread Starter Fat Tony

    (@wildpluckings)

    Invaluable guidance bcworkz. Thank you very much. I will be investing time following this closely and I will report back. It’s a new one and it’ll be an experience which I am anticipating will require a great deal of patience.

    I am used to working in MS Excel and making data do all kinds of backflips using custom functions. I do not think there is a problem I couldn’t resolve in Excel – (barring the most advanced vb script/external connectivity stuff)… if I could get 1/10th the grasp on SQL and php I believe I’ll be able to achieve my goals and from there – onward upward.

    • This reply was modified 4 years, 6 months ago by Fat Tony.
    • This reply was modified 4 years, 6 months ago by Fat Tony.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘developing an advanced data search function’ is closed to new replies.