tomusborne / generatepress

452 stars 89 forks source link

Post meta display issue in search template #558

Open tomusborne opened 1 year ago

tomusborne commented 1 year ago

Description

The post meta is displaying (or not) based on the first post in the loop.

Steps to reproduce

  1. Search for something that will show results from multiple post types.
  2. Depending on the post type of the first post, the post meta will show or not

Actual behavior

  1. The post meta display is based on the post type of the first post in the loop.

Expected behavior

The post meta display should always be based on the individual post type.

This is happening because we're checking get_post_type() in the wp hook, which will grab the post type of the first post only.

A potential solution:

add_action( 'generate_before_content', function() {
    if ( is_search() ) {
        $header_items = generate_get_header_entry_meta_items();

        $header_post_types = apply_filters(
            'generate_entry_meta_post_types',
            array(
                'post',
            )
        );

        if ( ! in_array( get_post_type(), $header_post_types ) && ! empty( $header_items ) ) {
            remove_action( 'generate_after_entry_title', 'generate_post_meta' );
        }
    }
} );

References

https://generate.support/topic/last-updated-date-in-internal-search-results/#post-28191 https://generate.support/topic/last-updated-date-in-internal-search-results/page/2/#post-28405