0

I have a website that has a blog and a custom post type called proyecto.

They are on mywebpage.com/blog and mywebpage.com/portafolio.Each of those pages have a search field and category dropdown selector.

Each page should only search and filter for their respective posts. Also, each of them has a custom search result layout. If I'm on mywebpage.com/portafolio the search results should only display proyecto post type results with its custom layout. The same goes for when I only select a category from the dropdown (and dont type anything), it should only display posts type proyecto that have the category selected and only show the categories assigned to proyecto post types, again, with its own layout.

At the moment the search and category filter work (Im not even sure if it works properly) but it merges both of the results (or doesnt show them all) and only displays them with the post design layout... and when I look for an empty seach,it always displays all of the proyecto posts and some of the post results with the proyecto layout.

Here is my code on both page-blog.php and page-proyecto.php:

<section id="search">
    <div class="container">
        <div class="page-title">PAGE TITLE</div>
        <form action="" method="post" class="search-form searchandfilter justify-content-between" id="searchbar">
            <div class="row">
                <div class="offset-lg-2 col-lg-6">
                    <label class="search-bar">
                        <div class="btn-search"><i class="fa fa-search"></i></div>
                        <input type="search" name="ofsearch" class="search-txt" placeholder="<?php echo esc_attr_x( 'Búsqueda', 'placeholder' ) ?>" value="<?php echo get_search_query()?>" />
                        <input type="hidden" name="post_type" value="post_type"/>
                    </label>
                </div>
                <div class="col-lg-4">
                    <?php
                    $categories = get_categories( array ('orderby' => 'name', 'order' => 'asc' ) );
                    if( $terms = get_categories( array('taxonomy' => 'category', 'orderby' => 'name') ) )
                    {
                        ?>
                        <select name="ofcategory" id="ofcategory" class="postform custom-select" name="categoryfilter">
                            <option value="0">Categor&iacute;a</option>
                            <?php
                            foreach ( $terms as $term )
                            {
                                ?>
                                <option value="<?php echo $term->term_id;?>" data-categoria="<?php echo $term->name;?>"><?php echo $term->name;?></option>
                                <?php
                            }
                            ?>
                        </select>
                        <?php
                    }
                    ?>
                    <input type="hidden" name="ofcategory_operator" value="and">
                </div>
            </div>
            <div class="row justify-content-end">
                <input type="hidden" name="ofsubmitted" value="1">
                <input type="submit" value="Filtrar" class="btn-submit btn-main-aqua">
                <div class="btn-submit btn-main-aqua" onclick="displaymessage();reset()">Vaciar Campos</div>
            </div>
        </form>
    </div>
</section>

NOTE:

  • This is the same code I use on both pages.
  • I only change the page title and value of the hidden input in the search.
  • I cannot know what the categories' names will be, they will be created by the user.

Here is my code on both search.php and category.php:

<?php get_header();?>
    <section id="search">
        <div class="container">
            <div class="row m-0"><div class="page-title">Resultados de b&uacute;squeda</div></div>
            <div class="row p-3"><div class="title">"<?php the_search_query();?>"</div></div>

            <div class="row row-eq-height m-0">
                <?php

                if(have_posts())
                {
                    if (get_post_type() == 'proyecto')
                    {
                        while (have_posts())
                        {
                            the_post();
                            $img = get_field("port-logo");
                            ?>
                            <div class="col-lg-4 col-md-6 col-sm-12 p-0" data-aos="fade-up" data-aos-duration="1000">
                                <a href="<?php the_permalink();?>">
                                    <div class="proyecto-img">
                                        <img src="<?php echo $img;?>">
                                    </div>
                                </a>
                            </div><!--  ITEM    -->
                            <?php
                        }
                    }
                    else
                    {
                        if (get_post_type() == 'post')
                        {
                            while (have_posts())
                            {
                                the_post();
                                $titulo = get_field("blog-titulo");
                                $subtitulo = get_field("blog-subtitle");
                                $img = get_field("blog-img");
                                $intro = get_field("blog-intro");
                                ?>
                                <div class="col-lg-4 col-md-6 col-sm-12" data-aos="fade-up" data-aos-duration="1000">
                                    <div class="post-cont">
                                        <a href="<?php echo the_permalink();?>">
                                            <div class="post-img">
                                                <img src="<?php echo $img;?>">
                                                <div class="subtitle"><?php foreach((get_the_category()) as $category){ echo '- ' . $category->cat_name; }?></div>
                                            </div>
                                        </a>
                                        <div class="desc-title"><?php echo the_title();?></div>
                                        <div class="post-desc"><?php echo $intro;?></div>
                                        <a href="<?php echo the_permalink();?>">Leer m&aacute;s</a>
                                    </div>
                                </div><!--  ITEM    -->
                                <?php
                            }
                        }
                        else
                        {
                            ?>
                            No results.
                            <?php
                        }
                    }
                }
                else
                {
                    ?>
                    No results.
                    <?php
                }
                ?>
            </div>
            <div class="row p-0 m-0 mt-5">
                <a href="javascript:history.go(-1)"><div class="btn-main-red">Regresar</div></a>
            </div>
        </div>
    </section>
<?php get_footer();?>

Im not sure if this is the correct way to code it or if I should have 2 different search results documents for that...

My main problem is that they function as if they were 1 same search form instead of 2 different (because that code is on each page with different values on the hidden field), same goes to the search.php. Should I make 2 different files, if so how should i o it?

I also tried using POST and GET and it did work but it makes my categories stop working...

I have been looking arround for an answer to my problem for days and even searched in the codex and various forums but I have not been successful, Im also a begginer so I dont know if I may have found the answer but I just didnt understand it... Any form of help adn guidance is apreciated, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.