post grid order by
-
CreatorTopic
-
April 23, 2014 at 3:20 pm #16435
smkorby
ParticipantI am creating a calendar with the post grid and home page with the same calendar as post grid slider. I would like to orderby starting with the next upcoming event. I have a visible custom field of “May 14, 2014” for example. If I add another field of orderdate with “20140514” can I use this to orderby? Where? Is there a better way?
-
CreatorTopic
-
AuthorReplies
-
April 23, 2014 at 4:27 pm #16442
Jason Bobich
KeymasterHello,
Yeah that could work. The hard part is though is that you need programmatically setup a custom query for your instances of the post grid from your child theme. Here’s an article on that:
http://dev.themeblvd.com/tutorial/extending-the-custom-query/
So you could do something like this in your custom query:
http://wordpress.org/support/topic/wp_query-order-by-post-meta-numeric-value
April 28, 2014 at 5:42 pm #16588smkorby
ParticipantThank you. I’m sure the answer is in there, but I’m not connecting all of the dots.
Child theme fuctions.php has this:
/** * Custom query for post lists/post grids */ function my_custom_posts_query( $query, $args ){ if( $args['query'] == 'grid_slider' ){ $query = array( 'order' => 'ASC', 'meta_key' => 'orderbydate', 'orderby' => 'meta_value_num' ); } return $query; } add_filter( 'themeblvd_posts_args', 'my_custom_posts_query', 10, 2 ); ?>
Each post in the Event category has a custom field called “orderbydate” with the date of the event entered as “20140514”. This is what I am trying to order by.
http://creativelyuncorked.com/calendar-2/ has this shortcode. Orderby does not work:
[post_grid category_name=”event” columns=”3″ rows=”0″ orderby=”orderbydate” order=”ASC” offset=”0″]I realize now that this probably should have been done with hooks, but before any of this, I had the content-grid.php in the child theme set to:
<?php /** * The template used for displaying posts in a grid. */ $excerpt = themeblvd_get_option( 'post_grid_excerpt', null, 'hide' ); $button = themeblvd_get_option( 'post_grid_button', null, 'hide' ); ?> <div class="grid-item column <?php echo themeblvd_get_att('size'); ?><?php if( themeblvd_get_att('counter') % themeblvd_get_att('columns') == 0 ) echo ' last'; ?>"> <div class="article-wrap"> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <div class="entry-content<?php if( $excerpt == 'show' && $button == 'show' ) echo ' has_elements'; ?>"> <div class="cdate"><?php echo get_post_meta($post->ID,'when',true) ?></div> <div class="soldout"><?php echo get_post_meta($post->ID,'soldout',true) ?></div> <?php themeblvd_the_post_thumbnail( themeblvd_get_att('location'), themeblvd_get_att('size') ); ?> <?php if( 'show' == themeblvd_get_option( 'post_grid_title', null, 'show' ) ) : ?> <h2 class="entry-title"><?php themeblvd_the_title(); ?></h2> <?php endif; ?> <?php if( 'show' == $excerpt ) : ?> <?php the_excerpt(); ?> <?php endif; ?> <?php if( 'show' == $button ) : ?> <p><?php echo themeblvd_button( themeblvd_get_local( 'read_more' ), get_permalink( get_the_ID() ), 'default', '_self', 'small', 'read-more', get_the_title( get_the_ID() ) ); ?></p> <?php endif; ?> </div><!-- .entry-content --> </article><!-- #post-<?php the_ID(); ?> --> </div><!-- .article-wrap (end) --> </div><!-- .grid-item (end) -->
None of these things is ordering by date. What am I missing?
April 29, 2014 at 12:58 am #16590Jason Bobich
KeymasterConceptually, you’re not understanding the article:
http://dev.themeblvd.com/tutorial/extending-the-custom-query/
You need to use the “query” option as a trigger for the custom query you want to create.
[post_grid category_name="event" columns="3" rows="0" orderby="orderbydate" order="ASC" offset="0"]
orderby="orderbydate"
isn’t anything WP Query knows. There’s nothing that exists called “orderbydate” and so this will never work. Instead use something as a catch for the custom query filter.For example, if you used “my-custom-query” as the catch, you’d be doing:
[post_grid query="my-custom-query"]
/** * Custom query for post lists/post grids */ function my_custom_posts_query( $query, $args ){ if( $args['query'] == 'my-custom-query' ){ $query = array( // your query here ... ); } return $query; } add_filter( 'themeblvd_posts_args', 'my_custom_posts_query', 10, 2 );
May 6, 2014 at 10:44 pm #16817smkorby
ParticipantThank you for explaining, it’s mostly working now. Last question, I promise. 🙂
What is it about this:
function my_custom_posts_query( $query, $args ){ if( $args['query'] == 'my-custom-query' ){ $query = array( 'order' => 'ASC', 'meta_key' => 'orderbydate', 'orderby' => 'meta_value_num' ); } return $query; } add_filter( 'themeblvd_posts_args', 'my_custom_posts_query', 10, 2 );
that only shows the first 5 posts?
[post_grid columns=”3″] shows all posts. If I add query=”my-custom-query” to the shortcode, only the next 5 posts show. I have orderbydate set in all posts.
This is in the calendar post now:
[post_grid query=”my-custom-query”]
[post_grid columns=”3″ rows=”99″]May 7, 2014 at 12:54 am #16822Jason Bobich
KeymasterI think it’s just that, by default, when you call a secondary query with
get_posts()
, WordPress will query five posts, unless you specify otherwise. Usually the theme takes care of this for you on the post grid by multiplying the columns by the rows. However, since you’re doing this custom query, you’re abandoning that.Just modify your code to query the amount of posts you want.
$query = array( 'order' => 'ASC', 'meta_key' => 'orderbydate', 'orderby' => 'meta_value_num', 'numberposts' => -1 );
-1
means to pull all posts available, but you could also specify an exact number, as well.May 7, 2014 at 1:21 am #16825smkorby
ParticipantYou. Are. Awesome. Thank you.
-
AuthorReplies
- The forum ‘The Arcadian Responsive WordPress Theme’ is closed to new topics and replies.