Ask your WordPress questions! Pay money and get answers fast! (more info)

Tags Search Results Page - Sorting WordPress

  • SOLVED

Hello,

I am trying to make my tag page ("<strong><em>/?tag=</em></strong>") to short the posts with the amount of tags.
The less tags a post have the closer it will be to the top (ascending order?!) (<em>if post have 1 tag it will be first. The post with 2 tags will be exactly after the 1st post. The post with 3 tags will be under the post with 2 tags etc</em>).

How can I do that? I tried editing the "<strong><em>sortby=</em></strong>" with some taxonomies and "<strong><em>order=</em></strong>" with <strong>ASC</strong> & <strong>DESC</strong> but no luck (in <strong>search.php</strong> & <strong>tag.php</strong>*).

*(The <em>tag.php</em> I created by myself)

Please note I do not have the <em>tag.php</em> file (the theme/tag search is working fine without it). I tried to make my own but I do not think it works correctly.

If this file is needed please help to create one. I created my theme with a software so I do not know how to create <em>tag.php</em> according to my theme correctly.

Thank you!

Answers (2)

2013-01-12

Arnav Joy answers:

which software you used to create your theme?


maximos comments:

Artisteer 4.0

Thank you for your time!

2013-01-15

Christianto answers:

Hi Maximos,

Please try this code:
[[LINK href="http://pastie.org/5687971"]]http://pastie.org/5687971[[/LINK]]

Put it on your functions.php
you need to use wordpress ver 3.5 or if you not using it, you can install [[LINK href="http://wordpress.org/extend/plugins/sort-query-by-post-in/"]]sort query by 'post__in' plugin[[/LINK]] to make it work.

Let me know if it doesn't work on your site


maximos comments:

I am running WordPress 3.4.2 (Russian. Version 3.5 is not available in Russian. It is the only reason I do not want ot upgrade). So, I installed the plug-in.

<strong>It says:</strong>
Parse error: syntax error, unexpected T_FUNCTION in DIR/functions.php on line 772

<strong>Line 772:</strong> usort($tags_connection, function($a, $b) {

Thank you for your time! I really appreciate it.


Christianto comments:

Please replace this line:
usort($tags_connection, function($a, $b) {
return (count($b) - count($a));
});

with this:
function tagsort($a, $b) {
return (count($b) - count($a));
};
usort($tags_connection, 'tagsort');



maximos comments:

The website is now available but when I am searching for a tag or tags it shows me this error:

Fatal error: Cannot redeclare tagsort() (previously declared in DIR/functions.php:772) in DIR/functions.php on line 772

The website is working well, I see the error in the results spot (where the results have to be, in the main table).


Christianto comments:

that is duplicate name of function,
you just have to change "tagsort" to something else,
for example "post_tagsort" or "custom_tagsort"
function post_tagsort($a, $b) {
return (count($b) - count($a));
};
usort($tags_connection, 'post_tagsort');



maximos comments:

Still the same error. When I changing the "post_tagsort" to anything, it shows me the same error. I tried custom_tagsort, post_tagsort_new, post_tagsortsss but still the same.

I marked with *** the items I tried to replace. Just in case I made a mistake.

function ***($a, $b) {

return (count($b) - count($a));

};

usort($tags_connection, '***');


Christianto comments:

Is the site is online?
could you PM me FTP access so I could check for it?

I tested it on my localhost with php 5.3 with no error..


maximos comments:

Can I have your email? or Skype?

Please note I want to change the "<em>?tag=</em>" page. I have 3 working search forms. The first on the left is what I need. It replaces the <em>?s=</em> with <em>?tag=</em>.


maximos comments:

Or do you want me to send you a message here, on WP Questions?


Christianto comments:

I send you my email address from wpquestion PM, please check your mailbox..


Christianto comments:

Oh Sorry Maximos, the functions should be declare outside tag_sorting_function(),

Please check this code:
[[LINK href="http://pastie.org/5689161"]]http://pastie.org/5689161[[/LINK]]

let me know if its still error..


maximos comments:

I replaced the previous code, with the one you gave me. The site and the search is working but the results looks the same before any code, like standard tag page. But sometimes it looks like they are in descending order by category. Would you like to take my cPanel credentials? When you done I would like to know the steps you took. Thank you very much. Waiting for your reply.

NOTE: When I am searching for one tag the URL looks like "/?tag=tag" and when I am searching for multiple tags the url looks like "/?tag=tag1+tag2". I am searching for tags in the search form with "space" separation like "tag1 tag2". Just in case I did not described correctly what I want. Thank you again!


maximos comments:

I received your email address. Do you want my login details?


Christianto comments:

Hi Maximos,


Please replace my code with this:
[[LINK href="http://pastebin.com/3msJh1M3"]]http://pastebin.com/3msJh1M3[[/LINK]]

I tested it on my localhost with multiple tag query ( ?tag=tag1+tag2 )
let me know if it didn't work as you wish..

Thanks


maximos comments:

It does not work. Between every post after search it shows me the error

Warning: Invalid argument supplied for foreach() in /functions.php on line 767

Line 767: foreach ($taxinfo[0]['terms'] as $term){

The posts are showed (not in needed order) but between them it shows me the error a wrote above.


Christianto comments:

the object $query->tax_query->queries seems different in wp version < 3.5

Please try this code:
[[LINK href="http://pastebin.com/NC8EdnhB"]]http://pastebin.com/NC8EdnhB[[/LINK]]


maximos comments:

Nope... Not working. I see no errors but the search results looks like the same, like default.

Thank you anyway...


maximos comments:

Hey, the script works on 3.5 like a piece of delicious cake (I'm just hungry :D)
I will try to upgrade my my 3.4.2 to 3.5. I will test it the next days and I it really works as I want to I will vote for you.

Thank you.


maximos comments:

code* my* if it*


Christianto comments:

have you install/activated [[LINK href="http://wordpress.org/extend/plugins/sort-query-by-post-in/"]]sort query by post__in[[/LINK]] plugin?

on wp < 3.5 this plugin is needed.


maximos comments:

Yes I had it installed but it was not working. I also tried to disable it but I had the same result. Now with wp 3.5 I didn't have this plug-in of course. As I checked it, for now it is working great. Please give me 1-2 days to test the code and I will upvote your answer. I will also share a few dollars with Arnav Joy. He also helped me a little.

Thank you again.


Christianto comments:

No problem Maximos..
I'm glad it work ;)


maximos comments:

Christianto,
While the post had no categories and had no text in the post (no content) but only tags it was working great. But since I added the categories and the content it stopped working. I mean, the most times a post with 1 tag appear after some posts with 3 tags and 4 tags. I made many combinations with my tags, I have about 8 tags and made many combinations with them like tag1+tag2 tag1+tag3 tag1+tag4 tag2+tag3 tag2+tag8 etc. And when I search for any tag (one or more) it shows me this problem... Maybe the categories affect the sorting?

PS: It's a fresh installation of wordpress. No plug-ins, no other codes attached.


maximos comments:

If it's cannot done with categories, I can accept custom fields too. But I would like to have tags...


maximos comments:

Sorry for commenting so many times. I found the problem. In the sort order affects the categories. I made two posts with one tag but the one had only one category and the seconds one had all the categories. After the search the post with one category was first but the post with all categories was last of all results but bust be one of the first because it has only one tag.


maximos comments:

Please check the code you gave me (~4 days before):
[[LINK href="http://pastie.org/5689161"]]http://pastie.org/5689161[[/LINK]]

It looks like it works but it's not in ascending order but in descending. The posts with more tags are first and the posts with less tags are last.


maximos comments:

I think it would be easier for you and me if I will give you my cPanel password. The problem is that the whole site is in russian. I don't know if it's okay for you.


Christianto comments:

Sorry forget to exclude categories, its saved on the same database table with tag..
please update with this:
[[LINK href="http://pastebin.com/NC8EdnhB"]]http://pastebin.com/NC8EdnhB[[/LINK]]


maximos comments:

Thanks... works good at first look. Will test it these days. Thank you again so much!


maximos comments:

Working like a charm! Will add 10$ to this question. Thank you.