WordPress.org

Forums

CPT's single page, gives 404 when search is in url (3 posts)

  1. thagizmo
    Member
    Posted 2 years ago #

    Hi,

    I've been running in circles to figure this out, so hoping that someone already had this kind of problem or at least can point me in the right direction. It's an extremely weird thing, so I'll try to explain it first.

    - I have a CTP, let's say "bread"
    - The url to the archive page is "/bread/"
    - When I search in this specific CTP, the url becomes "/bread/?s=query"

    So far for the basics. More about the CTP:
    - The "bread" CTP, has a custom field called "spread". It's plain text.
    - There are more fields added to the CTP and taxonomies as well. But I think they have no impact on this issue.

    So...when I search for a string that's present in the titles of one of the breads, the browsing let's me do this:
    > home > to bread archive page > "ENTER SEARCH" > bread archive page > "CLICK PERMALINK" > to bread single page > super...

    Now... when i search for a string that's present in the "spread" field, browsing does this:
    > home > to bread archive page > "ENTER SPREAD NAME" > bread archive page > "CLICK PERMALINK" > 404 ......eh??

    So when I get the 404, the ?s=myspread is there.... when I manually remove that from the query string, I get the normal single page for the CTP as if all is fine.

    Any clues? Must be the most complex way to explain a problem.....but still hoping anybody has a clue :)

    Cheers,

    Ben

  2. thagizmo
    Member
    Posted 2 years ago #

    Been able to find something more (no solution, but a better "problem"). Seems the custom field has nothing to do with it.

    The normal sql on a permalink page has this:
    SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = 'mybread' AND wp_posts.post_type = 'bread' ORDER BY wp_posts.post_date DESC

    When I search something in the query, the query becomes:
    SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = 'mybread' AND (((wp_posts.post_title LIKE '%mysearch%') OR (wp_posts.post_content LIKE '%mysearch%'))) AND wp_posts.post_type = 'bread' ORDER BY wp_posts.post_date DESC

    It seems what I am looking for is a way to kill the automatic url parsing for search queries, when I am on a "single-bread.php" page... should that be needed? I need to keep passing the query to the permalink page, because of a "back to search result page" link, that returns you to the archive page, with the same search string...

    Ben

  3. thagizmo
    Member
    Posted 2 years ago #

    For others:

    I'm not feeling really happy with it, but I did mange to work around it somewhat. I still have a feeling that I'm fundamentaly doing something wrong regarding setup of the theme... but for now it will work.

    I added an action to my theme that will filter out the "keyword" parameter whenever I'm on a "single" page through:

    function remove_search_keyword_from_query( $query ) {
        if ( $query->is_single() && $query->is_main_query() ) {
            $query->set( 's', '' );
        }
    }
    add_action( 'pre_get_posts', 'remove_search_keyword_from_query' );

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags