• sonny_b

    (@sonny_b)


    Hi.
    How would one go about adding a new option to ‘orderby=’ when querying posts?
    I have a custom field named ‘crew_order’ (where the meta_value is a single number), and whilst I’ve found various scraps of code to call in the MySQL data (such as here https://www.ads-software.com/support/topic/121011?replies=15), it’s not quite what I’m looking for, as I need to keep it as part of a custom query_string.

    How, and where, would I go about adding “orderby=crew_order” as a function?

    Many thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • MichaelH

    (@michaelh)

    Might look at Displaying_Posts_Using_a_Custom_Select_Query and adjust the code accordingly.

    Also, Otto has info for adding an orderby value to query_posts in this thread:
    https://www.ads-software.com/support/topic/157515?replies=5#post-699291

    Thread Starter sonny_b

    (@sonny_b)

    Thanks, I’ll check those out.

    Thread Starter sonny_b

    (@sonny_b)

    Fantastic, I got it working with the following code:

    <ul class="crewlist">
    				<?php
    					 $querystr = "
    						SELECT * FROM $wpdb->posts
    						LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    						LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    						LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    						WHERE $wpdb->term_taxonomy.term_id = 4
    						AND $wpdb->term_taxonomy.taxonomy = 'category'
    						AND $wpdb->posts.post_status = 'publish'
    						AND $wpdb->postmeta.meta_key = 'crew_order'
    						ORDER BY $wpdb->postmeta.meta_value ASC
    						LIMIT 9
    					";
    					  $crew_order = $wpdb->get_results($querystr, OBJECT);
    					?>
    <?php if ($crew_order): ?>
      <?php foreach ($crew_order as $post): ?>
        <?php setup_postdata($post); ?>
    				<li><?php if ($currentId!==$post->ID) { ?><a href="<?php the_permalink();?>"><?php the_title(); ?></a><?php } else { ?><?php the_title(); ?><?php } ?></li>
      <?php endforeach; ?>
      <?php endif; ?>
    				</ul>

    Is there any way to get it to auto paginate once the LIMIT has been reached?

    Thanks so much for the help.

    Thread Starter sonny_b

    (@sonny_b)

    Regarding pagination –
    I found some great help from Aesqe here:

    https://www.ads-software.com/support/topic/154300?replies=7

    But I can’t get it working with the Paginav plugin. Anyone have any thoughts?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding a new orderby= function’ is closed to new replies.