Support

Account

Home Forums Front-end Issues Sort posts based on a repeater field "Start_date"

Solved

Sort posts based on a repeater field "Start_date"

  • I have a course page i which I use a repeater field called “sub_seminars” with date pickers “start_date” & “End_Date” inside to display course start and end date.

    Now in my archives page I want to sort the courses based on the start_date which has a meta_key of sub_seminars_0_start_date.

    I get the posts sorted if I use this code

    $posts = get_posts(array(
    	'post_type'			=> 'dt_portfolio',
    	'posts_per_page'	        => -1,
    	'meta_key'			=> 'sub_seminars_0_start_date',
    	'orderby'			=> 'meta_value',
    	'order'				=> 'ASC'
    ));

    However I lose the category filter. I just takes the posts from dt_portfolio and sorts them out. I want it to sort the posts that are generated by individual taxonomy values.

    e.g sort posts with venue los-angeles

    my archive page looks like this

    ?php
    
    $posts = get_posts(array(
    	'post_type'			=> 'dt_portfolio',
    	'posts_per_page'	        => -1,
    	'meta_key'			=> 'sub_seminars_0_start_date',
    	'orderby'			=> 'meta_value',
    	'order'				=> 'ASC'
    ));
    
    /**
     * The main template file.
     *
     * This is the most generic template file in a WordPress theme
     * and one of the two required files for a theme (the other being style.css).
     * It is used to display a page when nothing more specific matches a query.
     * E.g., it puts together the home page when no home.php file exists.
     * Learn more: http://codex.wordpress.org/Template_Hierarchy
     *
     * @package presscore
     * @since presscore 0.1
     */
    
    // File Security Check
    if ( ! defined( 'ABSPATH' ) ) { exit; }
    
    $config = Presscore_Config::get_instance();
    $config->set( 'template', 'blog' );
    $config->set( 'layout', 'list' );
    $config->set( 'template.layout.type', 'list' );
    $config->set( 'post.preview.media.width', 30 );
    
    get_header(); ?>
    
    			<!-- Content -->
    			<div id="content" class="content" role="main">
    
    				<?php if ( have_posts() ) : ?>
    
    					<div class="articles-list">
    					
    						<?php do_action( 'presscore_before_loop' ); ?>
    
    						<?php update_post_thumbnail_cache(); ?>
    
    						<?php while ( have_posts() ) : the_post(); ?>
    								                                                
                                                     <?php
    							// populate config with current post settings
    							presscore_populate_post_config();
    
    							presscore_get_template_part( 'theme', 'blog/list/blog-list-post' );
    							?>
                                                  																	                                                  
    						<?php endwhile; ?>
    
    						<?php do_action( 'presscore_after_loop' ); ?>
    
    					</div>
    
    					<?php dt_paginator(); ?>
    
    				<?php else : ?>
    
    					<?php get_template_part( 'no-results', 'blog' ); ?>
    
    				<?php endif; ?>
    
    			</div><!-- #content -->
    
    			<?php do_action('presscore_after_content'); ?>
    
    <?php get_footer(); ?>

    Any kind of help is appreciated.

  • WP will automatically sort posts based on taxonomy/term by using the correct link and instead of creating a custom query to get the posts you use a pre_get_posts filter https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts.

    The main problem you’re going to have is sorting the posts by a repeater field. You say that the repeater sub field is sub_seminars_0_start_date, but if you have multiple repeater rows then you will also have sub_seminars_1_start_date, sub_seminars_2_start_date, sub_seminars_3_start_date, etc. This is where you’re going to have a problem. It is virtually impossible to sort posts based on a repeater sub field value, unless you repeater will only ever have a single row or if you only care about the first row.

  • You are right, I do have these fields. However I only want to sort the posts based on the first meta key sub_seminars_0_start_date
    I don’t bother about the other fields. Just this one

  • Like I said, WordPress create URLs for each of the terms in the taxonomy. These pages automatically only show the posts for a specific term.

    What you are doing here is overriding the query that WP has already done

    
    $posts = get_posts(array(
    	'post_type'			=> 'dt_portfolio',
    	'posts_per_page'	        => -1,
    	'meta_key'			=> 'sub_seminars_0_start_date',
    	'orderby'			=> 'meta_value',
    	'order'				=> 'ASC'
    ));
    

    You either need to add a taxonomy query to this in order to recreate what WP is already doing… this is a waste, will take more work, and will slow down your site because you’re doing multiple queries when they are not needed….

    … or you can set up a pre_get_posts filter https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts to add the new sort order to the query that WP is already performing.

    
    
    	
    // add to functions.php	
    function order_dt_portfolio_by_repeater($query=false) {
    	
    	// only used on main query on front end of site
    	if (is_admin() || !$query || !is_a($query, 'WP_Query') ||
    		!$query->is_main_query()) {
    		return;
    	}
    	
    	// modfiy a custom post type to show and order by meta value
    	if (isset($query->query_vars['post_type']) &&
    			$query->query_vars['post_type'] == 'dt_portfolio') {
    		$query->set('meta_key', 'sub_seminars_0_start_date');
    		$query->set('orderby', 'meta_value');
    		$query->set('order', 'ASC');
    		$query->set('posts_per_page', -1);
    	}
    } // end function blunt_pre_get_posts
    add_action('pre_get_posts', 'order_dt_portfolio_by_repeater');
    
  • Thanks for the reply John but this didn’t work for me. The order remains still the same !!

  • Then you’ll also need to add a tax query to your query. See the WP_Query() doc https://codex.wordpress.org/Class_Reference/WP_Query

    I can’t say how you’ll need to incorporate this, but it will involve figuring out what taxonomy/term WP is already using.

    Either that or there’s some debugging you’ll need to do in what I’ve provided. The code I provided was not tested, it was put together based on the information you’ve given.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Sort posts based on a repeater field "Start_date"’ is closed to new replies.