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

Custom Query By jQuery Var WordPress

  • SOLVED

I am trying to do a custom dynamic sorting function for a theme.

Here is how I am currently doing the sorting: [[LINK href="http://snipt.org/kloW/"]]http://snipt.org/kloW/[[/LINK]] - <strong>THIS WORKS GREAT, I DO NOT WANT TO CHANGE THIS.</strong> I already have the ability built to sort by title, number of comments, a few custom fields, etc. (that is all built in WP), which is the easy part.

What I want to be able to do is sort by a jQuery var that is NOT a custom field or anything in the post meta. I have a “voting system” made for each post (on the category page) & each post has a unique voting system. Using jQuery I can targeted the number of votes the post has by finding the “span” that displays the number. So basically, I have got to the point where I have pulled just the number as a var.

<strong>Well, where I am stuck is, is it possible to use that var made (a number) to do a custom “orderby” query in WP based only off that var (and work with the ASC/DESC), which is NOT a custom field or anything inside the post meta?</strong>

<strong>I want to be able to put this in my current way of sorting ([[LINK href="http://snipt.org/kloW/"]]http://snipt.org/kloW/[[/LINK]]).</strong>

Please Let me know if this is not 100% clear on what I mean or I am trying to do and I can try to further explain! Thanks! :)

NOTE: I already have built in ASC and DESC as well.

My testing page is: http://shirtlaunch.com/category/testing123/ you can see how I already have the dynamic sorting in, currently. (I haven’t formatted it all yet)

Answers (3)

2010-10-07

Tom Ransom answers:

What isn't clear from your post - is where the votes are stored within the post - a custom field?

There are jQuery plugins that will let you dynamically resort the divs based on a value. e.g http://stackoverflow.com/questions/1603617/sorting-divs-in-jquery-by-custom-sort-order

So one alternative is to write the page and then sort the divs if selected.

If you are looking to modify the WP_query based on the vote count - that would have a lot to do with where/how the vote count is stored.


Zach Reed comments:

Well the votes are being stored in a whole different system using SQLite, not related to WP at all. It uses this php script to do the voting: [[LINK href="http://codecanyon.net/item/thumbsup/50411"]]http://codecanyon.net/item/thumbsup/50411[[/LINK]] - and to make a "voting box" you have to have unique values, so I put the voting box in the loop and take a post's id to make each voting box unique.

That is why I thought I would have to use a jQuery variable like:
var numbers = $('.thumbsup').find('form').find('a').find('span').text();

to pull that number and then somehow sort the post's via that number.


Tom Ransom comments:

Ok - now I have a better understanding - for the case that you describe - you will need to create your own sorting function and resort $wp_query->posts using a custom function after executing your query.

There is an excellent article here with sample code:
http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields

Your function used with usort needs to retrieve the current vote count from the $thumbsup object for each of the two posts passed into the routine
http://www.geertdedeckere.be/lab/themeforest/thumbsup/wordpress/


Zach Reed comments:

I am not exactly sure what you mean...

This may help, this is my loop for the category page: [[LINK href="http://snipt.org/klpJ/"]]http://snipt.org/klpJ/[[/LINK]]

And this is the sorting script I have: [[LINK href="http://snipt.org/kloW/"]]http://snipt.org/kloW/[[/LINK]]

And I target the number in the thumbsup output with this jQuery function (if that even matters?)
var numbers = $('.thumbsup').find('form').find('a').find('span').text();


could you possible give me an example of what you mean?


Tom Ransom comments:

Here is some example code:
http://snipt.org/klpN/

We are adding a new option - Votes and a new switch case.
In the new switch case - we need to do a default wp query then resort the response based on the vote count.


Zach Reed comments:

Okay I copied your code but sorry if this is a dumb question... I get this error...

Fatal error: Call to undefined method ThumbsUp::SOMEFUNCTION()

I take it I need to replace "SOMEFUNCTION", but with what? That VAR?


Tom Ransom comments:

I don't have access to the thumbsup script. But you are inserting the vote template by giving each thumbs up/down element an id that includes the post ID.

It is almost a given that there is a function within the thumbsup script that given that identifier - returns a numeric vote count. You'll have to look at the documentation/code to figure that part out.


Zach Reed comments:

I found the identifier in the Thumbsup template but now I get:

Fatal error: Cannot access protected property ThumbsUp::$item'

If I could somehow give it access (not sure how?) would that be bad? Would it be some kind of security hole or something?


Zach Reed comments:

Not sure if this helps but <?php defined('THUMBSUP_DOCROOT') or exit('No direct script access.') ?> is in the Thumbsup template, at the top.


Tom Ransom comments:

You are looking for a function within the thumbsup class that will return the count given the identifier.


Zach Reed comments:

I think I did that:

global $thumbsup;
$vote_count_1 = $thumbsup->$item['results']['positive_votes']($post1->ID); // Get the vote count for post1
$vote_count_2 = $thumbsup->$item['results']['positive_votes']($post2->ID); // Get the vote count for post2
return ($vote_count_1 < $vote_count_2) ? -1 : 1;


But with that I get: "Fatal error: Cannot access protected property ThumbsUp::$item"

In the Thumbsup template at the top it has, <?php defined('THUMBSUP_DOCROOT') or exit('No direct script access.') ?>


I think it may have something to do with that? Thoughts?


Zach Reed comments:

I think I may have got it... Let me test it out in the AM (it is after 3am my tine) and I will let you know how it goes! :)


Zach Reed comments:

We are so close....

I fixed that "cannot access protected property error" so now,
<?php echo $thumbsup->item['results']['positive_votes'] ?>
will output the item's vote number.

The problem I think now is the "sort_posts_by_vote" function, I currently have:
function sort_posts_by_vote($vote_count_1, $vote_count_2){
global $thumbsup;
$vote_count_1 = $thumbsup->item['results']['positive_votes'][$post1->ID]; // Get the vote count for post1
$vote_count_2 = $thumbsup->item['results']['positive_votes'][$post2->ID]; // Get the vote count for post2
return ($vote_count_1 > $vote_count_2) ? 1 : -1;
}


And that makes this:
http://shirtlaunch.com/category/testing123/?by=votes&dir=asc

But as you can see, it isn't sorting right. I think if we just tweak that function it will work! :) We are sooooo close! haha

p.s.
You have already "won" this question for all of your great help, we are so close to getting it right. I also want to make sure to get your PayPal to send you a few extra bucks, I didn't realize how complex this was, so I feel like $15 is not worth all the time you have put in to help, but we can talk about that after we get this done, I will "message" you! :) (JUST AN FYI!)


Tom Ransom comments:

You are getting the error because you are NOT using a function - you are using a array variable marked as private.

Looking for a method within the class that returns the vote count. Work backwards from the section that outputs the values - it has to retrieve it from somewhere. You need to access the same code.


Tom Ransom comments:

If you'd like to send me the thumbsup script via email - I can probably find it pretty quickly.

transom at 1bigidea dot com

2010-10-07

Eddie Moya answers:

So, the numbers in those elements with the vote numbers, that number must be generated and stored somewhere. Where is that? Based on that we might be able to come up with some method of sorting them.

On the otherhand, purely from a front end JS prospective. You could have jquery simply resort them live on the page based on the numbers, or to answer your question of orderby direct - i suppose you could try to do some sort of ajax thing.


Zach Reed comments:

Check what I told "transom" above, please.

2010-10-08

John Cotton answers:

Wouldn't you be better to sort on the client side using jQuery?

Add an attribute of votes to each parent element (the DIV with classes "grid_4 entry clearfix"), the use this nice element sorting code here:

[[LINK href="http://james.padolsey.com/javascript/sorting-elements-with-jquery/"]]http://james.padolsey.com/javascript/sorting-elements-with-jquery/[[/LINK]]

Stick a fade out and is either side of the sort and you'll have a cool Web 2.0 interface.

And it would get rid of a post back as well saving you server load.

Of course, once you have this code, you could add HTML attributes for title, date, cusom fields etc and use the client side code to do <em>all</em> your sorting! No postbacks at all!


Zach Reed comments:

I don't like that method because then there is no permalink for the sorting.

Example with the way I am doing it now and "transom" is helping with is I can have a permalink like http://shirtlaunch.com/category/testing123/?by=votes&dir=asc for sorting, as well.


John Cotton comments:

I take your point Zach, but there's nothing to stop you using those values in the query string to initialise the client side code so that the page that gets sent is set to the state they require and then works from there on using the client side.

I just thought it would nicer for the visitor to have it all done without postbacks (and perhaps so nice jQuery slides as the content moves around :)).

JC


Zach Reed comments:

Well that would be pretty cool, and I might have to take you up on that. Me and the "transom" guy have just been working on this other way for probably 4+ hours and I feel like he has "won" the question with his great help. And I also have probably pulled out all my hair by now trying to get the other way to work! haha :) It so closeeeee. lol

But that is good to know that, that way is available, what is your email just in case I want to contact you about doing something like this later?