How do allow users to change the order of posts, based on custom fields?
For example :
First custom field: Value1, second custom field: Value2.
In archive page, the posts are presented in order of the value 1 by default, and a filter (links) allows to order them according to the value 1 or 2.
Use the pre_get_posts hook as per the example here https://developer.wordpress.org/reference/hooks/pre_get_posts/#comment-2571. This assumes that your custom fields are stored as post_meta.
Note that you need to be very careful that you are only affecting the query that you want to affect at the time that you want to affect it. In this example they are checking that they are not in admin, that they only doing events (in your case you want posts) and that it is the main query (not another query that say a widget or some other function is running). Consider limiting it to the page that you want this to run on or putting the code in the theme template, so that it only runs for that template and not at any other time.
Related
I have a webgallery (made with laravel) and would like to add the possibility to reorder the images... Now, I have thought of several approaches but for every aproach i find that there should be a better way of doing it.
the gallery does not use javascript, so ones changes have been made it needs to be sumbitted and reloaded to reflect the changes
The main difficulties are:
how to store the order in the database? an additional Integer column?
how to add a picture "between" two others?
how to handle it at a frontend level?
So far the best ideas I had are these:
a column with integers, order by clause on this column. Frontend: a move up and a move down button.
problems of this solution: it needs a refresh after each single movement. it needs to identify the previous/next picture and swap the number with that one. To move a single pic from the end of the gallery to the top it takes forever.
a column with integers, automatically prefilled in steps of 100, order by this column + upload time in case of same numbers, Frontend: a textbox where the user can specify the integers for each picture and a submit button.
problems of this solution: does not look very professional. solves all the problems of the previous solution
same as previous solution but with double values to be able to insert pictures without limits.
They all dont seem the real deal.. Any sugestion on how to do it properly is welcome. thanks
I have done that kind of sorting in OpenCart products list (custom backend design)
Sort order was additional column order INT(11) in database
We had 3 input fields: up/down/custom
Where custom was dropdown of all indexes from 1 to max-items.
All inputs does the same:
Take new order value and shift all elements except itself. Up or down shift depends if you move element to front or to back of current position
UPDATE order FROM products SET order = :newOrder WHERE id = :currentItemId
if ($newOrder > $oldOrder)
UPDATE order FROM products SET order + 1 WHERE order >= :newOrder AND id != :currentItemId
else
UPDATE order FROM products SET order - 1 WHERE order <= :newOrder AND id != :currentItemId
Inserting does the same update, just first query becomes INSERT INTO
To get rid of ugly refresh of page on every action we do Ajax requests and re-sorted DOM with jQuery
I have a Drupal 7 module which includes a database query that gives me a list of jobs id's in a specific order based on calculated distance from a user-inputted location (nearest first, furthest last).
I then pass the job id's to views_embed_view as arguments with a contextual filter on the view. This then displays a list of jobs to the user.
However, whilst there is no sorting specified within the view, the order gets changed automatically. e.g. if the order of the records passed to views_embed_view is 10,52,22 it displays in the following order: 10,22,52 i.e. it gets ordered numerically ascending.
Any ideas on how to get views_embed_view to stop re-sorting my results?
Just thought I'd post how I solved it in case anyone encounters this issue in future:
A Drupal module called Views Arguments Extras includes a sort handler which allows for the order of items to be based on their order in a multi-value argument:
https://www.drupal.org/project/views_arguments_extras
Many thanks to the contributors of this module.
I know I can extract Sales Order data in OpenCart database, in table shop_order and shop_order_product.
Is it possible to differentiate which user creates the sales order?
I don't think there's an option to identify which admin user created the order. You may need to code it yourself. The below steps may help you.
Add 2 new fields created_user_id and updated_user_id in order table - with default value as 0.
Update the admin/model/sale/order.php to insert values to those fields as $this->user->getId(); (addOrder and editOrder functions ).
Add a new field user_id in order_history table - with default value as 0.
Update the admin/model/sale/order.php to insert value to that field as $this->user->getId(); (function: addOrderHistory).
You can do these by directly modifying the files or using vqmod. If it's done via vqmod then it'll be easier for you to make changes during opencart version upgrade.
Opencart: Vqmod tutorial
Have a nice day!!
By default, there's no way to know which admin an order is created by, or even that the order isn't created by the user (other than the IP would be that of the admin). You could in theory add the user_id to the shop_order table, and pass that to the manual order editor when it creates the orders
Is there a way to call field rows in a URL without using the column name??
So I currently have a posting site where users can select category or subcategories of choice from drop downs, how it's currently setup my site outputs links to the categories chosen such as..
topics.php?category=Food&sub_cat=Pies
topics.php?sub_cat=Pies
This allows users to go to either one of the links, or both
topics.php?category=Food&sub_cat=Pies
To give more functionality I am looking at adding textboxes instead of drop downs, the problem is users will more than likely enter the data in different boxes than other users, ie.
User 1. catbox: Food subcatbox: Pies
User 2. catbox: Pies subcatbox: Food
So in this case my current URL system won't return accurate results, so my question is would there be a way where "category" or "subcategory" could be replaced and just put the results together without them being listed in 2-5 different fields therefore not returning all the results that = to that value? "food" or "pie" in this example.
topics.php?xxx=Food&xxx=Pies
or
topics.php?xxx=Pies&xxx=Food
Looking at So homepage if you click "php" it will put php in the URL, click mysql and it will put "php+mysql" that sort of thing.
you can use parent child method in your database.your table would be like this
id - parent_id - category_name - depth
when you want to insert a data to your table it's depth will be one plus it's parent depth
when someone post to your page you first take query witch of the inputs has most depth then that will be your subcategory.
Calling field rows via parameters in your URL may be a very bad idea. It's a perfect way to allow a massive SQL injection attack. So, the answer is probably "yes, but HOLY MOLY PLEASE DON'T!"
Now it may be that your code is parsing these out on the back end and protecting them via any of a variety of methods, I can't tell from the amount of code posted.
i'm using custom fields in wordpress to store meta values. some custom fields have multiple values. i'm retrieving the array with "get post meta" which returns an array, as expected. but it seems the different items are ordered without any logic. some show up in the order they were entered, other's in opposite order, some total chaos.. what could i be missing?
i can't change the way the items are stored anymore.. there is too many entries in the database and the values show up perfect in the editing area but are stored in different order within the array.
This may or may not be possible. Wordpress is a bit like free energy machines - it violates a few useful concepts.
All your metadata is stored in the postmeta table. This table has a few fields: meta ID, post ID, meta key, value. Every time you add a meta, you add a row to this table. Every time you update a meta, however, you do not change the row order.
get_post_meta usually returns rows in the same order, so I am guessing you are doing some sort of sorting somewhere. Could we see some code? If it's always in ascending or descending but never as a random mix, you have a sort() lost somewhere.
I just recently had this same issue, here's what I found:
When you use get_post_meta(), WordPress checks if the meta data for the given object (post) has alreadby been loaded; if it has, then it's on the object cache (volatile, apc, memcached, etc) and load it's from there.
If the meta data it's not on the object cache, then it loads all the meta data for that object through update_meta_cache(), which queries the database without an ORDER BY
If your storage engine it's MyISAM, the results will be returned on a random order every time you update the meta data, but when using InnoDB the order in which the results are returned seems to be consistent (at least on my tests)