• Greetings;

    I’ve launched a website using one of WordPress premium themes, and used some plugins to enhance the functonality of it. As of now, I need to write a query for a part of the website, and since I’m not a WP developer, I’m gonna need some help on it.

    The website’s content mostly consists of categorized images. I’ve installed and used an addon to be able to create different taxonomies for my Media Library. So far I have Field, Technique, and Customer as my taxonomies.They all have their own entries like Field1 Field2 Field3, Tech1 Tech2 Tech3, and Customer1 Customer2 Customer3 and so on … . So, every time I upload an image to the library, I check relevant options from each of those 3 taxonomies for that image.

    Later on, I installed and used another plugin for advanced search. By using that plugin, I’m able to show those 3 taxonomies on front-end, and by choosing desired options, users are able to see the relevant images. To give you a better look, when you open the advanced search page, you see 3 drop-down menus each representing those 3 taxonomies and all of their defined options. They are not dependent on one another.

    But I need more complex and advanced type of search. Let me give you an example.
    Let’s consider we have 10 images uploaded to the library. For 8 of those images, Field2, Tech1, Tech2, Customer2 and Customer3 are checked on back-end, and for the other 2, Field1, Tech2, Tech3 and Customer 1 are checked for instance. As you can see, there’s no single image for which the combination of Field2 and Tech3 are checked.
    I want the search feature to work this way, that, when user checks Field2, only Tech1 and Tech2 are selectable and Tech3 gets disabled, simply because there’s no image uploaded with Field2 and Tech3 yet.

    To sum it up, I’d like to create some sort of dependency only on the front-end. Something like this : Field > Technique > Customer.

    Helps are appreciated in advance.

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

    (@bcworkz)

    These are all taxonomy terms, there are no metadata or custom fields that are part of any queries, correct? (A “Field” taxonomy is easily confused with a normal post’s “Custom Field”. As long as you can keep it straight, it’s all good).

    In the media library, be sure you understand that images are never searched for. Instead, an image’s attachment post is searched for. From found attachments, we can get the actual images. Thus you can use the WP_Query class to do your searches. Just be sure the “post_type” argument is “attachment”. You will mainly be using the “tax_query” argument to specify which attachments to find. tax_query can be extremely complex because of how you can nest arrays together and have them relate in different ways.

    You say your search fields are not dependent on each other, but in fact you need the list of certain selection elements to be dependent upon the previous field and what attachments are found using the previous field. When the first field is selected, query for all attachments that are assigned the selected term. Then compile a list of distinct terms that go into the next field. You can gray out terms that are not in the list, or simply don’t output such terms, only output terms that are in use.

    Upon the second selection, you can again make a query using the two terms and compile a distinct list of terms to use in the third field. When the third field is selected, make the final query matching all three terms.

    This sort of UI is best accomplished by Ajax techniques. Ajax in WP has a few quirks. Be sure you are able to send simple data to server and back via Ajax before you get involved with your fields and queries.

    Thread Starter Duvenhage

    (@duvenhage)

    Greetings @bcworkz and thanks for your detailed response.

    I’m familiar with PHP coding, so I could partially guess what needs to be done.
    But the problem is I have zero knowledge of WP Database structure and WP-specific functions.
    So I kinda needed to know if someone can help me out on the query itself.
    I’m perfectly aware that complex coding can’t be done for free anywhere, but I was and still am willing to know if what I need is just a few lines of coding, or something much bigger than what I thought, and also if someone can provide me with the query itself (if it’s only just a few lines of coding). Because on that entire website, it’s the only thing remained unsolved.

    Thanks again,
    Regards

    • This reply was modified 6 years, 4 months ago by Duvenhage.
    Moderator bcworkz

    (@bcworkz)

    I appreciate that you recognize that quality coding does not come for free. Let’s be sure this topic does not devolve into solicitation of work for pay or else we’ll need to close the topic.

    You don’t need to know much of DB structure to use the WP_Query class. That’s its purpose — to add a useful layer of abstraction between your PHP and the DB. There are a number of examples on the WP_Query Codex page. In particular note the multiple taxonomy handling example. You can add as many taxonomy argument arrays as you need. You can even nest them to achieve complex logical relations.

    An overall example of using WP_Query is available if you scroll up to the top of the page I’ve linked to. Here’s an example of WP_Query arguments to get attachments with both Field2 and Tech1 terms assigned:

    $query = new WP_Query([
      'post_type'=>'attachment',
      'tax_query' => array(
    		'relation' => 'AND',
    		array(
    			'taxonomy' => 'field',
    			'field'    => 'slug',
    			'terms'    => 'Field2',
    		),
    		array(
    			'taxonomy' => 'technique',
    			'field'    => 'slug',
    			'terms'    => 'Tech1',
    		),
    	),
    ]);

    To get valid customer terms from those attachments, loop through the posts returned by the query and collect all customer terms into an array. If the term is already in that array, naturally you don’t add it again. You should only want one of each term assigned in order to use it to compose the final dropdown form for getting images matching terms of the three taxonomies.

    This should be enough to get you going. If you still have trouble, show us what you have so far and someone should be able to set you straight. As long as you are simply seeking coding assistance and not looking for freebie coding, people are usually willing to help out.

    • This reply was modified 6 years, 4 months ago by bcworkz. Reason: code fixed
    Thread Starter Duvenhage

    (@duvenhage)

    Hello and thanks alot for the big help @bcworkz, I really appreciate it.
    I’ll put it into use and see the outcome. I hope I can get it all working by myself.

    Regards

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom complex query’ is closed to new replies.