Tagged: x
-
AuthorPosts
-
November 21, 2016 at 11:01 pm #1266410
Hey There, big thanks for all your help with my questions.
Is it possible to sort the portfolio items by a custom field?
I tried using the code mentioned in this post: https://community.theme.co/forums/topic/sort-portfolio-alphabetically/#post-314894 changing ‘title’ to ‘last_name’ the name of the custom field I created but that did not seam to work.
I’m ultimately trying to sort portfolio items alphabetically on last name. (I’m using the portfolio function as a staff listing.)
November 22, 2016 at 12:32 am #1266471Hi there,
Kindly follow this thread – https://community.theme.co/forums/topic/sort-portfolio-alphabetically-2/#post-65391
Cheers!
November 22, 2016 at 2:53 pm #1267314Thank you for pointing me in that direction. I looked at that post and a few others and came up with the following to sort the portfolio in alphabetical order based on a custom field, “last_name”, used on portfolio items . Unfortunately, I’m doing something wrong and it’s not working. Any help is greatly appreciated.
add_action( 'pre_get_posts', function ( $query ) { if ( x_is_portfolio() ) { $query->set( 'order', 'ASC' ); $query->set( 'meta_key','last_name'); $query->set( 'orderby', 'meta_value' ); } });
November 22, 2016 at 11:18 pm #1267773Hi There,
Would you mind providing us with login credentials so we can take a closer look on your settings? To do this, you can make a post with the following info:
– Link to your site
– WordPress Admin username / password
– FTP credentialsDon’t forget to select Set as private reply. This ensures your information is only visible to our staff.
November 23, 2016 at 1:14 pm #1268552This reply has been marked as private.November 23, 2016 at 7:33 pm #1268918Hello There,
Thanks for providing the information. Your code did not work because pre_get_posts is called after the query variable object is created, but before the actual query is run. You have to modify the layout portfolio page template to make this work. Since you have your child theme active and ready, please follow the following steps below:
1] Using Notepad or TextEdit or Sublime Text or any text editor, please create a new file in your local machine.
2] Insert the following code into that new file<?php // ============================================================================= // VIEWS/GLOBAL/_PORTFOLIO.PHP // ----------------------------------------------------------------------------- // Includes the portfolio output. // ============================================================================= $stack = x_get_stack(); $entry_id = get_the_ID(); global $sitepress; if ( function_exists( 'icl_object_id' ) && is_callable( array( $sitepress, 'get_current_language' ) ) ) { $wpml_post = get_post( icl_object_id( $entry_id, 'page', false, $sitepress->get_current_language() ) ); $entry_id = $wpml_post->ID; } $paged = ( is_front_page() ) ? get_query_var( 'page' ) : ( ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1 ); $cols = get_post_meta( $entry_id, '_x_portfolio_columns', true ); $count = get_post_meta( $entry_id, '_x_portfolio_posts_per_page', true ); $filters = get_post_meta( $entry_id, '_x_portfolio_category_filters', true ); switch ( $cols ) { case 'One' : $cols = 1; break; case 'Two' : $cols = 2; break; case 'Three' : $cols = 3; break; case 'Four' : $cols = 4; break; } ?> <?php x_get_view( 'global', '_script', 'isotope-portfolio' ); ?> <div id="x-iso-container" class="x-iso-container x-iso-container-portfolio cols-<?php echo $cols; ?>"> <?php if ( count( $filters ) == 1 && in_array( 'All Categories', $filters ) ) { $args = array( 'post_type' => 'x-portfolio', 'posts_per_page' => $count, 'paged' => $paged, 'order' => 'ASC', 'meta_key' => 'last_name', 'orderby' => 'meta_value' ); } else { $args = array( 'post_type' => 'x-portfolio', 'posts_per_page' => $count, 'paged' => $paged, 'order' => 'ASC', 'meta_key' => 'last_name', 'orderby' => 'meta_value' 'tax_query' => array( array( 'taxonomy' => 'portfolio-category', 'field' => 'term_id', 'terms' => $filters ) ) ); } $wp_query = new WP_Query( $args ); ?> <?php if ( $wp_query->have_posts() ) : ?> <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <?php if ( $stack != 'ethos' ) : ?> <?php x_get_view( $stack, 'content', 'portfolio' ); ?> <?php else : ?> <?php x_ethos_entry_cover( 'main-content' ); ?> <?php endif; ?> <?php endwhile; ?> <?php endif; ?> </div> <?php pagenavi(); ?> <?php wp_reset_query(); ?>
3] Save the file named as
_portfolio.php
4] Upload this file to your server in the child theme’s folder
wp-content/themes/x-child/framework/views/global/
Hope this helps. Please let us know how it goes.
November 24, 2016 at 8:53 am #1269588THIS IS AMAZING! BIG THANK YOU. Love this theme, love the support even more. (for those using this php work I added a comma “,” after ‘meta_value’ on line 58 – I think.
November 24, 2016 at 9:45 am #1269630Thanks for the good words. It means a lot to us. 🙂
-
AuthorPosts