Wordpress search including multiple categories - wordpress

I have a search form that has a text field and 3 drop downs, that have categories. What I'm trying to achieve is the current search query that posts appears as follows:
?s=Milk&cat=4&cat=7&cat=15.
What I'm trying to achieve is something like:
?s=Milk&cat=04,7,15
I'm relatively new to WordPress and PHP and haven't been able to find any good documentation or references to what I need to do to accomplish this.
I don't necessarily want the answer, just some guidance in the right direction.

This was already answer: multiple categories query
It must be like this:
/?cat=970,971,972 where the # is the cat id.

Related

Generate and display html table with user data from wordpress

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?

Wordpress order categories

I have a Wordpress site where a lot of the categories are peoples names. The slug becomes john-smith fred-wilkinson etc by default. So the category list is in alphabetical order by their first name. I would like them to list by last name.
I could get the authors/editors to change the slug to wilkinson-fred etc but I would rather have a solution that they don't have to alter the default slug.
I know there are plug-ins that you can drag and drop to any order you want. Again this relies on the author/editor putting it in the right place.
My slugs currently look like this
john-smith
fred-wilkinson
Alan-t-jones
etc
I have other categories that I don't want to alter. I only want to reorder where the parent category is "artist".
Any ideas? If I put wilkinson-fred in the description field can I sort on that?
Thanks
I assume that you want to change the order of wp list categories function output (maybe make it a little clearer in your future SO questions!). If that's so, I think you have two options here:
1) Hijack the orderby parameter as mentioned in this answer by #Chris_O at Wordpress Stackexchange:
There is an unused column, term_order, in the wp_term_relationships table that you can use to assign a custom order to the terms within your taxonomy. The order is set at 0 by default and it will take a custom query to get the order back and another solution to set the order.
Although as #Kaiser mentions in the comments to the answer, this sollution is a little hacky as it's not meant to be used this way and its implemantation may change in future releases of Wordpress.
2) Store the output of the function in varible (echo => false under $args), manipulate the output and print the resulting array. This is the way I would choose as it makes you more in control of the situation.

Drupal related articles view with list of nodes to exclude from results

I'm trying to create a list of related articles in Drupal, based on shared tags. I've used this solution and it works great:
Creating list of similar nodes in Drupal 7/Views 3
However, I wish to expand on this and exclude certain nodes from the related view which are not relevant. I have a node reference field (field_exclude) for the article which allows the user to select which nodes to exclude.
I just need to update this related view to exclude any node ids found in field_exclude. Any idea on how I update the view to add this functionality?
Thanks.
Add a Filter Criteria for the field_exclude and there set the "Not empty" value or any other value that wil do the job.
Another solution here is to use the NodeQueue module where users can add nodes in lists/views.

Drupal view to list nodes and comments

I want to create a view that shows the latest posts in a forum and also any latest comments. The comments and the posts would all show in the same view. Is it possible for me to do this?
Thanks in advance,
Ben
In essence: no. Views requires you to choose one main resource in the first step: you there (amongst others) choose to go with either nodes, or comments.
However, with some (ugly) configuration, you can load the comments that go with nodes. Each result would look like, Node - Comment, e.g.:
Can I have cheesburgers - First!
Can I have cheesburgers - No, I was first!!!111oneone
Can I have cheesburgers - LAME.
and so on. With some styling, you can then get it to show comments and nodes in separate rows.
However, this is ugly and hackish. My advise: write a simple module that either exposes a block, or a menu+page, and do two simple (and light) queries on the database: SELECT nid, title, ... FROM {nodes} LIMIT 10 and SELECT nid, name AS title, ... FROM {comments} LIMIT 10 then mix these two up. Or, with some (more complex) SQL magic, you could even join the two tables and create fancy results that e.g. order by created date of either nodes or comments.
With Drupal 7 you can add a relationship Last comment and then add that field to the view.
One idea I've seen is to use Views Custom Field to "attach" a wholly separate view with PHP code. See comment #4 for the code (in a request for this feature in Views. for a code example).

Drupal 6 & Views 2 - DISTINCT field

I'm using the Feeds module to import lots of Feed Item nodes. Due to a malformed feed file, I'm getting lots of duplicates. I'm using a View to display these nodes, and need to be able to add a DISTINCT filter on the "Node: Post Date" field, so I only get 1 result for each post-date.
I will also look into tackling the problem at the source so to speak (I don't want to have all those duplicates in the first place), but this is an interesting issue in itself - I can't find a way to add a DISTINCT filter on a field other than the Node ID (which has it's own option in the View's Basic Settings box).
I found a great article on a good way to alter the SQL queries that are generated from views before they get executed: http://echodittolabs.org/blog/2010/06/group-views. I used this to basically suffix a GROUP BY clause to the end of the query (in a really nice, clean and versatile way).
As an aside, I also found a way to tackle the issue of importing lots of duplicate feed items, the details of which are here: http://drupal.org/node/661314#comment-3667228. It adopts quite an extreme approach (deleting all items before each update), but this is the only solution for some nasty malformed feeds.
I was holding out for some undiscovered feature of Views that let you do this, but I don't think there is one - maybe in the next version ;)
There are two option to solve this:\
apply this patch
OR
hook_views_query_alter => just paste
$query->distinct = 1;
$query->no_distinct = 'views_groupby';
I guess you have two options: either put some logic in the view template file to skip the duplicate items or implement hook_views_query_alter() to change the query used by the view, adding the DISTINCT clause.
We found this issue in drupal 6.x view - had 7 of 150 items duplicated one or twice. No idea why. Issue only appeared for anonymous users. Luckily, views 6.x.2.16 provides a 'distinct' setting under the basic settings, I set it to Yes and got rid of the duplicates.

Resources