Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sorry in advance for my limited PHP skills.

I saw this post and the function helped to fix my pagination problem for the archives pages for my custom post type. pagination doesn't show up for custom post type

The challenge I have is I need this same type of function to account for ordering the archive in ASC order and by a custom field value which is tied to a DATE value I created.

I'm trying to figure out how to write the function by grabbing code from other places and trial and error. I'm hoping someone here can point me in the right direction. Thank you for your thoughts.

RESOLVED... SORT OF

I added these other $query->set parameters and it seems to work for the archive listing

/CUSTOM ARCHIVE FOR EVENTS/ function events_pagesize( $query ) { // exit out if it's the admin or it isn't the main query if ( is_admin() || ! $query->is_main_query() ) return; // so its not admin, and its the main query, is it the tr-events post archive? if ( is_post_type_archive( 'tr-events' ) ) { // it is!! Set the posts_per_page to 6 $query->set( 'posts_per_page', 4 ); $query->set( 'order' , 'asc' ); $query->set( 'meta_key', 'wpcf-tr-order-date'); $query->set( 'orderby', 'meta_value'); return; } } add_action( 'pre_get_posts', 'events_pagesize', 1 );

While not ideal, for other page templates, this works for the Custom Post Type Archive page.

share|improve this question

1 Answer 1

Have you consulted the Codex WP_Query() entry?

To query by custom field, you'll need to add tax query parameters to your query args.

e.g.

'meta_key' => 'foobar'

To sort, you'll need to add sort and orderby query parameters to your query args.

e.g.

'order' => 'ASC',
'orderby' => 'meta_val_num'
share|improve this answer
    
I've tried this, but it seems to break the pagination on the archive page. –  cidotcom Feb 26 '13 at 16:07
    
Here's the function I'm using /*CUSTOM ARCHIVE FOR EVENTS*/ function events_pagesize( $query ) { // exit out if it's the admin or it isn't the main query if ( is_admin() || ! $query->is_main_query() ) return; // so its not admin, and its the main query, is it the tr-events post archive? if ( is_post_type_archive( 'tr-events' ) ) { // it is!! Set the posts_per_page to 6 $query->set( 'posts_per_page', 4 ); $query->set( 'order' , 'asc' ); return; } } add_action( 'pre_get_posts', 'events_pagesize', 1 ); I'm trying to order by meta_val –  cidotcom Feb 26 '13 at 16:11
    
Please add code to your original question, for clarity/syntax highlighting. –  Chip Bennett Feb 26 '13 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.