Support

Account

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

  • 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');