I have implemented normal search functionality in simple pages using php and MySQL where I get result rows by mysqli_fetch_array. There I could have any numbers of variable with AND or OR relationship and it will retrieve results from the db.
Now I am trying to do this in WordPress.
I have seen solutions where one uses WP_query to search by term names. How do I achieve this AND and OR variables in my search query.
For example, my query is like select all post title where term name like 'search keyword' AND term slug like 'hi'.
I have created a view terms_and_posts table from merging terms and posts tables using taxonomy relationship, with columns custom term name, custom term slug, post title, post content, post status, and post type.
Do I need a merged table or it can be achieved by modifying WP_Query?
I do not want to resort to using plugins as I am very new to WordPress, and I prefer not to use plugins as long as I can help it.
Related
I'm trying to filter blog posts based on 2 or more categories.
The blog posts are a content type and the categories are as well. Each blog post can have only one category. The category is connected to the post via a reference field. I'd like the user to be able to filter the posts. The user can select multiple categories at once.
It seems I'm unable to fabricate the query. Here's what I have so far:
// PHP
$categories = ["79RwpuYXo4W9FiYMdpeShj", "4CAkZRYSa3EB23ipTwZ92R"];
$query = (new Query)
->setContentType('blogPosts')
->where('fields.postCategory.sys.id', $categories, 'in'); // using 'all' instead of 'in' also doesn't return any results
In my mind, this should get all blog posts that hold a reference to either category entry (id). However, no entries are returned using this query. I'm using contentful/laravel v4.0.
Okay, I figured it out. I'm using Contentful Core v2. The correct query structure for v2 is the following:
// PHP
$categories = ["79RwpuYXo4W9FiYMdpeShj", "4CAkZRYSa3EB23ipTwZ92R"];
$query = (new Query)
->setContentType('blogPosts')
->where('fields.postCategory.sys.id[in]', $categories);
I am developing a taxonomy system (tags, categories, and attributes) with Laravel. Currently in my database I have the following tables, "terms", "term_relationships" and "products". The "term_relationships" table being the pivot table. However, with multiple taxonomies (e.g. categories, tags and attributes), how can I store the taxonomy names without getting them confused between categories, tags or attributes. I see that WordPress uses a 4th table named "term_taxonomy", however I am unsure of how the 4 tables relate to each other.
Would someone be so kind as to enlighten me on this subject providing the best solution to implement such a taxonomy system.
Thank you.
If this can help, I created this package : https://github.com/codiiv/laravel-taxonomies and it's based on the 3 table structure in Wrordpress, but I combined them into one table rather!
I'm designing a blog database. I want posts to belong to any number of categories, including none (i.e. number of categories = 0, 1, 2, 3, ...).
I understand that the common way to design such a database (e.g. in Wordpress), is to have one table for posts, one table for categories, and one table for relationships, thus:
table relationships
column relationship id
column post id
column category id
But this means that to display a post, my script will have to make at least three database queries. This seems slow to me.
Which is why, in another blog, I had only one table for posts which included a varchar column for categories, in which I inserted a string with all the category names, which I parsed in PHP, thus
table posts
column post id
... (many other columns)
column categories
where column categories contained a string that might look like this:
apples,oranges,bananas
which I simply explode()ed in PHP.
Please explain why I should avoid the second method (one table, explode). There must be something wrong with it that I miss, because it is not commonly used in blog software.
Note:
There might still be a table listing categories, into which new categories are written when a post is created, and from which lists of categories are drawn to display them in, of example, the sidebar.
I expect there to be many more queries for posts than for posts-in-categories, which is why I don't worry much about querying the second database for posts from a certain category, which might be faster in the first database.
In second case you will get huge problems with finding post by some category.
For example, you write posts about programming languages and want to show all post about python, php, ruby, etc on separate pages ... but you can't write simple and quick request to database because you violates 1 normal form in your second database scheme.
JimL has already mentioned JOIN which allows to make 1 request and get all needed information from standard many-to-many relationship scheme with link table post2category
I have a working system running on oracle db. Inside that I have a set of employees under different categories,sub categories etc.,. Now I have to build a system in drupal fetching these data+additionals. I planned to setup the category sub category etc, with the taxonomy and TAC in drupal. I have a set of SP for fetching data from oracle. My question is how can I relate the taxonomy terms with those category ids(Employees are classified under category ids, so I have to keep track of the taxonomy relates to the categories in oracle). I'm new to drupal. Please help me guys..
Perhaps you could add a field that stores the corresponding oracle id to the vocabulary in Drupal? That is if you cant name the terms directly so that they match the records in oracle.
I've used the following modules in the past to solve similar tasks. Perhaps they can be of use to you.
https://www.drupal.org/project/feeds
https://www.drupal.org/project/feeds_tamper
I'm trying to make form to post advert in newspaper and want to let user select style of future post and show how it will be printed. So i create taxonomy vocab. "style" with name, sample image and max. characters to this type.
Next i'm creating field taxonomy term relation in advert content type.
Now i need to show in adding form not just radios or select with titles but title, sample image and other fields from taxonomy.
I think i need to use hook_form_alter so i use mymodule_form_ad_node_form_alter(&$form, &$form_state, $form_id) but what i can't understand is how in drupal way i can get rest of taxonomy fields? In $form array is only tids and titles.
Is it possible not to query DB again but setup existing query to retrieve all fields i need?
Also how i can insert that additional information into form? Is it other way than #suffix #prefix on radios?
If this is a Drupal 7 then you can use an Entity Reference field along with the Entity Reference view widget instead of a term reference field. That will allow you to specify a view to use to populate the field options, and you can create an entity reference view that display the additional taxonomy term fields you want to display.