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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$15
Custom Query By jQuery Var

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

Here is how I am currently doing the sorting: http://snipt.org/kloW/ - THIS WORKS GREAT, I DO NOT WANT TO CHANGE THIS. 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.

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?

I want to be able to put this in my current way of sorting (http://snipt.org/kloW/).

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)

This question has been answered.

Zach Reed | 10/07/10 at 7:58pm Edit


(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    10/08/10
    12:07pm
    Tom Ransom says:

    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.

    • 10/07/10 10:04pm

      Zach Reed says:

      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: http://codecanyon.net/item/thumbsup/50411 - 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.

    • 10/07/10 11:00pm

      Tom Ransom says:

      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/

    • 10/07/10 11:17pm

      Zach Reed says:

      I am not exactly sure what you mean...

      This may help, this is my loop for the category page: http://snipt.org/klpJ/

      And this is the sorting script I have: http://snipt.org/kloW/

      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?

    • 10/07/10 11:50pm

      Tom Ransom says:

      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.

    • 10/07/10 11:56pm

      Zach Reed says:

      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?

    • 10/08/10 1:07am

      Tom Ransom says:

      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.

    • 10/08/10 1:14am

      Zach Reed says:

      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?

    • 10/08/10 1:17am

      Zach Reed says:

      Not sure if this helps but

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

    • 10/08/10 1:48am

      Tom Ransom says:

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

    • 10/08/10 2:07am

      Zach Reed says:

      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?

    • 10/08/10 3:09am

      Zach Reed says:

      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! :)

    • 10/08/10 10:51am

      Zach Reed says:

      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!)

    • 10/08/10 11:11am

      Tom Ransom says:

      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.

    • 10/08/10 11:12am

      Tom Ransom says:

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

      transom at 1bigidea dot com

  • avatar
    Last edited:
    10/07/10
    9:47pm
    Eddie Moya says:

    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.

    • 10/07/10 10:05pm

      Zach Reed says:

      Check what I told "transom" above, please.

  • avatar
    Last edited:
    10/08/10
    4:32am
    John Cotton says:

    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:

    http://james.padolsey.com/javascript/sorting-elements-with-jquery/

    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 all your sorting! No postbacks at all!

    • 10/08/10 10:48am

      Zach Reed says:

      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.

    • 10/08/10 10:54am

      John Cotton says:

      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

    • 10/08/10 10:58am

      Zach Reed says:

      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?

    • 10/08/10 10:59am

      John Cotton says:

      john@dynamicarray.co.uk

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.