• Resolved morfe1

    (@morfe1)


    Hi Aldolat

    I was trying to find a way to display posts, related to the url of 404 page.
    So if you access /journey, which doesn’t exist and returns a 404 page, ideally it’s supposed to display posts suggestions based on the search=”journey”.

    I’ve found that one can use shortcodes and search option, but I don’t know if it’s possible to pass the URL value as the search parameter.

    I’ve managed to get it working somehow:

    <?php 
    	$test123 = $_SERVER['REQUEST_URI'];
    	$url = trim($test123, '/');
    		echo $url;
    		echo do_shortcode("[pissc search=\"$url\" orderby=title number=5]"); 
    ?>

    But the results are a bit different from the normal search query –

    Tried changing the orderby to title/name with no difference. Debug is enabled.

    Am I missing something? Any help is greatly appreciated.

    Regards,
    Alex

    • This topic was modified 7 years, 5 months ago by stephencottontail. Reason: fixing HTML
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Aldo Latino

    (@aldolat)

    Hello @morfe1,
    it’s possible that the search function of WordPress makes something different from what Posts in Sidebar does. My plugin passes a simple parameter to WP_Query and WP_Query returns some results. Probably the search function does a different work. I didn’t investigate into this, that seems to be an interesting aspect.

    Regarding the code you are using, if I enter a url in your site like [your-domain]/store/takeaway, the string passed to the shortcode is store/takeaway that will not find anything. Try this instead:

    
    $request_uri = $_SERVER['REQUEST_URI']; // e.g. /blog/curabitur
    // echo '$request_uri is <code>' . $request_uri . '</code><br />';
    preg_match( '/[^\/]+$/', rtrim( $request_uri, '/' ), $search );
    $search = implode( '', $search );
    // echo '$search is <code>' . $search . '</code><br />';
    echo do_shortcode('[pissc search="' . $search . '" orderby=title number=5 debug_query="true"]');
    

    Remove the double slashes to view the content of the variables.

    This code will get only the last word of the request uri, so the string /blog/blog/blog/wordpress will be transformed into wordpress.

    Thread Starter morfe1

    (@morfe1)

    Hi @aldolat

    Thanks for the code and explanation, you’re the best!

    I think it should be easier to pass the $request_uri of 404 page to the wordpress’ search function, rather than replacing your plugin’s search functionality. Moreover I’m going to use some plugins for better search experience.

    But a better search could be a nice feature for Posts in Sidebar plugin
    or your future plugin (something like Posts in 404 page, huh).

    Regards,
    Alex

    Plugin Author Aldo Latino

    (@aldolat)

    ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Displaying Related posts on 404 page with shortcode’ is closed to new replies.