Site logo

[Support request] Search results grouped by the different custom post types

Home Forums Support [Support request] Search results grouped by the different custom post types

Home Forums Support Search results grouped by the different custom post types

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2578792
    Jaume

    Hi. I’ve created 3 custom post types, so now I have 4 post types in my site: posts, opinions, dictionary and companies. When I search a word, the results appear all mixed up, sorted by date, but I’d like to have them sorted by each post type.

    I’m using the INFO template from the site library and it comes with the search results block, but I can’t figure out how to achieve what I need. Do I have to modify the search results block to have a loop for each custom post type? In this case, how (because I cannot find where I can change this loop)?

    How do I have to do this? Thank you very much.

    Raquel

    #2579102
    Ying
    Staff
    Customer Support

    Hi Raquel,

    I’m using the INFO template from the site library and it comes with the search results block

    I built the Info template, but I don’t recall there being a block called search result block.

    Can you explain a bit more or point me to the search result block you are referring to?

    Achieving what you are looking for will require custom PHP coding.

    Can you try this?

    function search_filter($query) {
        if ($query->is_search && !is_admin()) {
            $query->set('post_type', array( 'post','opinion','dictionary','company'));
    	$query->set('orderby', 'type');
            $query->set('order', 'ASC');
    		
        }
        return $query;
    }
    
    add_filter('pre_get_posts','search_filter');

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    #2584081
    Jaume

    Hi Ying. This is not exactly what I need to do. Let me try to explain. When I serach for a word, I need the results to show like this:

    POST
    ——–
    All results from post.

    OPINION
    ————
    All results from opinion.

    DICTIONARY
    ————-
    All results from dictionary.

    COMPANY
    ————
    All results from company.

    I understand I need to create a custom results block, but I don’t know how. Any idea? Thanks.

    #2584457
    Ying
    Staff
    Customer Support

    I understand I need to create a custom results block, but I don’t know how

    There’s no way to use block to achieve what you are looking for unfortunately.

    #2585044
    Jaume

    Not even using custom PHP?

    #2585072
    Fernando
    Customer Support
    #2585085
    Jaume

    No, sorry, this is not what I want to do: this code just shows me results from this 4 post types, ordered by date (as it is supposed to be shown, in the other hand). But my client wants the results page ordered by post type like this:

    POST
    ——–
    All results from post.

    OPINION
    ————
    All results from opinion.

    DICTIONARY
    ————-
    All results from dictionary.

    COMPANY
    ————
    All results from company.

    Is there a way, using functions? Or should I repurpose the search.php page?

    #2585120
    Fernando
    Customer Support

    Can you provide the link to the site in question?

    Keep the code by Ying added. We’ll check what’s occurring.

    #2585171
    Jaume

    Let me ask my client if I can send you the password, because now is behind a protection.

    #2585194
    Fernando
    Customer Support

    I see. Let us know.

    You may use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information

    Note: This isn’t controlled by GeneratePress by the way. It’s controlled by WordPress. It’s somewhat out of our scope of support.

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