WordPress.org

Ready to get started?Download WordPress

Forums

Sort post via term (10 posts)

  1. Leave Stick
    Member
    Posted 1 year ago #

    How would i sort the post via term using the class WP_Query?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    You will not be able to do this if posts are assigned more than one term. How would you know which one to use for the sort?

    If your posts are assigned only a single term, it may be possible to do this using filters to modify the query. Here is an article that shows example filters: http://wordpress.mcdspot.com/2010/05/30/filters-to-modify-a-query/

    You would set the global variables to the clauses necessary to change the query before issuing the call to WP_Query, and reset them to blank after the call.

  3. Leave Stick
    Member
    Posted 1 year ago #

    Would you give me an example for it and how the code looks like? Sorry but im not as expert in wordpress like you.

    For example i do have a term word 'foo' how could i use the wp_query class to achieve this.

  4. vtxyzzy
    Member
    Posted 1 year ago #

    Is 'foo' the term, or the taxonomy? If it is a term, there is no need to sort on it because you would select all posts with that term.

    If it is a taxonomy, here is a pastebin with sample code to sort by the terms of a taxonomy.

  5. Leave Stick
    Member
    Posted 1 year ago #

    No...no...no... It's my fault. Not specific term btw.

    What i really mean is to sort all post via there term.

  6. vtxyzzy
    Member
    Posted 1 year ago #

    See the pastebin link that I posted earlier for some sample code.

  7. Leave Stick
    Member
    Posted 1 year ago #

    It throws me an error. Do you have another one.

  8. vtxyzzy
    Member
    Posted 1 year ago #

    That code cannot be used 'as is'. It must be adapted to your theme. It is only meant as an example.

    The exact coding will depend on your theme.

  9. Leave Stick
    Member
    Posted 1 year ago #

    Nope. The error is thrown by core wordpress php file which is query.php

  10. vtxyzzy
    Member
    Posted 1 year ago #

    I just copied the code and tested it with no errors. Please put your code in a pastebin and post a link to it here.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags