Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

custom sort on archive page of custom post type with custom fields

  • My site has an Events plugin with a custom field for the event’s event_date. The archive page sorts by the post date, but I want to sort the posts, based on the event_date. I think I would make a localized copy of the archive template, moving it to my generatepress_child theme, with a custom filename of filename-events.php However, I’m having trouble finding the correct file to bring in ie the one with the query that I am looking to modify. This is the code that I am looking to implement.

    <?php
    // Original query
    $args = array(
        'post_type' => 'post',
        'orderby'   => 'date', // Sort by post date
        'order'     => 'DESC', // Descending order
    );
    
    // Modify query to order by custom field 'event_date'
    $args['meta_key'] = 'event_date';
    $args['orderby'] = 'meta_value';
    $args['order'] = 'ASC'; // or 'DESC' depending on your preference
    
    $query = new WP_Query( $args );
    ?>
  • Hi there,

    You should not need to create a specific CPT archive template, you can use WP’s pre_get_posts filter for this. Give this PHP code a try:

    function custom_order_cpt_archive_by_event_date($query) {
        // Check if on the main query and if it's for a CPT archive
        if ( ! is_admin() && $query->is_main_query() && is_post_type_archive('your_custom_post_type') ) {
            // Set the meta key and orderby parameters to order by the "event_date" meta field
            $query->set('meta_key', 'event_date');
            $query->set('orderby', 'meta_value');
            $query->set('order', 'ASC'); // Adjust the order as needed (ASC for ascending, DESC for descending)
        }
    }
    add_action('pre_get_posts', 'custom_order_cpt_archive_by_event_date');
  • Thanks, Ying!

    Worked like a charm and is localized to this post type. https://wwgri.com/Events/

    I haven’t worked with archive pages a lot, and I have one additional goal…On the page, under “Meetings”, but above the listing, I’d like to add some into text. Would I use an Element for that, or do you have other ideas about the best, most sustainable approach?

    Thanks again. Always great to work with the GP team!

  • Yes, you can use a block element – hook to insert the content you want.

    The hook name is after_archive_title, and the location would be Events archive.

    For more info about hooks, check this visual hook guide: https://docs.generatepress.com/article/hooks-visual-guide/

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.