• Dear,

    I would like to switch my joomla site to wordpress but I am facing a problem.
    I added a custom field with ACF (Advanced Custom Fields)

    I would like to sort my posts by date of the event and not by date of publication.

    my custom field is: start event

    I added the get-post in my file
    wp-content> theme> template_child> functions.php

    $posts = get_posts(array(
    	'post_type'			=> 'event',
    	'posts_per_page'	=> -1,
    	'meta_key'			=> 'start_event',
    	'orderby'			=> 'meta_value',
    	'order'				=> 'DESC'
    ));
    
    if( $posts ): ?>
    	
    	<ul>
    		
    	<?php foreach( $posts as $post ): 
    		
    		setup_postdata( $post )
    		
    		?>
    		<li>
    			<a>"><?php the_title(); ?> (date: <?php the_field('start_event'); ?>)</a>
    		</li>
    	
    	<?php endforeach; ?>
    	
    	</ul>
    	
    	<?php wp_reset_postdata(); ?>
    
    <?php endif; ?>

    but it doesn’t work.

    Can someone tell me which code to paste and where because I have no control over coding.

    Thanks a lot!!!!

    • This topic was modified 4 years, 2 months ago by bcworkz. Reason: code fixed

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Try 'orderby' => 'meta_value_num',

    If that doesn’t work, your dates may be in a difficult to sort format. They need to be either a UNIX timestamp or yyyy-mm-dd format.

    If some other format, the SQL DATE() function may help, it’ll require creating a custom SQL query or using the “posts_request” filter.

    Is that code in a function? Or is it directly in the functions.php file?
    You might want to read this section of the docs: https://developer.wordpress.org/reference/functions/get_posts/#more-information

    Keep in mind that in WordPress, the theme doesn’t perform the main query. WordPress figures out what to query and then loads the theme’s template file to handle the results of that query. You can make template files for custom post types by naming them correctly. See the Template Hierarchy.

    Thread Starter sortir06

    (@sortir06)

    @bcworkz : Thank you for your help
    I am using a yyyy-mm-dd format.
    ‘orderby’ => ‘meta_value_num’, doesn’t work either.

    @joyously : Thank you for your help
    Yes, the code is directly in the functions.php file
    I also tried to modify the file: wp-includes / post.php but without success either.

    I am surprised to see that such a “basic function” is not developed in wordpress natively. I also did not find a pluggin.
    I will look into the documentation hoping to find some leads …
    If you have any ideas, please let me know. Thank you

    If you read at the link I gave, it will tell you how to change the query using the pre_get_posts filter, but you should make sure you only apply it when it is a query for events, not posts.

    Since your code is directly in the functions.php file, it is run when the theme is loaded, which is not the right time to do code like that.

    Thread Starter sortir06

    (@sortir06)

    @joyously Thank you very much, it helps me a lot to know that it is not at the level of the theme that we must act.
    The problem with notices is that it is for people who already know where to put get_posts and other codes. It is rarely well explained for non programmers

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Order articles posts by custom field’ is closed to new replies.