Jonnie Grieve Digital Media: Blog

Home
by on 26th February, 2021 - 4:36pm (0)

Blog: R6D72: #100DaysOfCode (More Posts)

Day 71

As it stands when  I start to write this piece, is about 10am and I’m just beginning my new day working on the search.php template of my blog, in tandem with the WordPress Starter repository that I’ve also been building.

I said that my efforts with WordPress search had been beset with problems, just one of them being that the page always says it returns no results no matter what results (or none) appeared on the screen. Which I didn’t really understand because this conditional statement was functioning before and giving me what I thought was an accurate representation of the actual results returned. This is what happens when as I often do, I find myself going down a coding rabbit hole.

Maybe WordPress simply doesn’t do it, I thought. Maybe WordPress search, for Custom Post Types simply isn’t going to cut it for my purposes, which is to get search results only for “Blog Post” Custom Post Types. What I had was returning results based on post titles only.

It seemed like another case of multiple searches on Google with little reward.  And then finally the breakthrough came when I called found_posts directly against the WP Query I’d set for retrieving blog posts. This sounds so simple now but when nothing works out for you, suddenly the simple things in coding are not so simple anymore.

So to round off where I’m currently at with my exploration of WordPress search

So thankfully I was able to cross a few things from my list.

search.php h3 heading colours

Yes. Currently showing the colour white which is off-kilter from the rest of the design and that’s something that needs customising with styling. To give more context to this task I do need to spend some time cleaning up the SASS behind the scenes so it is easier to maintain.  That is after all what SASS is there for. And now that I’ve implemented more WordPress features into the blog, this is what I’ll get into doing next.

Return the search term that was searched by the user

Yes. Achieved using the WordPress method the_search_query(). Relatively straightforward. Often times with these methods it’s a case of learning when these methods use their own markup and when to wrap it in HTML markup of your own.

Return the correct count of results sent back as an integer.

Yes. They match what is returned in the above integer. In this loop, I’ve used methods to return the post title and the custom post type blub with  the_field(“article_blurb”).

I have also filled in the title tag elements with a condition statement.

<title>
        <?php 
            if( $page_title === "Home" ) {
                echo $page_title . " | Blog for Jonnie Grieve Digital Media"; 
            } else if ( $page_title === "blog_post" ) {
                echo $page_title . " | Blog for Jonnie Grieve Digital Media"; 
            } else { 
                echo $page_title . " | Blog for Jonnie Grieve Digital Media"; 
            }
        ?> 
</title>

Allow the user to make a new search from the search result page.

I’m still thinking over when and where it’s appropriate to ask the user to submit another search given that what they probably want to see more, is the list of results that they get. But this is certainly something I can look at in the future. For now, they will be invited to make another search on any previous search that returns no posts.

This post has been assigned to the following categories

    Leave a Reply

    Your email address will not be published. Required fields are marked *