Skip Navigation

[Resolved] Search on custom post type

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 5 replies, has 3 voices.

Last updated by Minesh 6 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1135509

We are trying to setup a search on a custom post type based on a custom field selection in the search form.

We can get it to work fine when no custom field is selected but it's not returning results when a custom field is selected.

The code for the search form looks like this:

<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<div class="box">
<label for="search-text">Search</label>
<div class="search-fields">
<div class="search-text">
<input type="text" name="s" id="search-text"><input type="hidden" name="post_type" value="publication" />
</div>
<div class="search-in">
<div class="search-in-selected"></div>
<span class="arrow"></span>
<div class="search-in-dropdown">
<ul class="no-list">

  • <input type="radio" name="search-in" value="all" id="search-in-all" checked><label for="search-in-all">All</label>
  • <input type="radio" name="search-in" value="Article" id="search-in-article"><label for="search-in-article">Articles</label>
  • <input type="radio" name="search-in" value="News" id="search-in-news"><label for="search-in-news">News</label>
  • </div>
    </div>
    </div>
    <div class="search-submit">
    <input type="submit" name="submit" value="Search">
    </div>
    </div>
    </form>

    The code on our custom search page looks like this:

    <?php
    $search_in = $_GET['search-in'];

    if ($search_in == "Article") {
    $args = array( 'post_type' => 'publication', 'meta_query' => array(array('key' => 'wpcf-news_type', 'value' => 'Article', 'compare' => 'LIKE')), 's' => $s, 'showposts' => -1 );
    }

    else if ($search_in == "News") {
    $args = array( 'post_type' => 'publication', 'meta_query' => array(array('key' => 'wpcf-news_type', 'value' => 'News', 'compare' => 'LIKE')), 's' => $s, 'showposts' => -1 );
    }

    else {
    $args = array( 'post_type' => 'publication', 's' => $s, 'showposts' => -1 );
    }

    query_posts( $args );
    $total_results = $wp_query->found_posts;

    ?>

    What am I doing wrong when it comes to including the custom field wpcf-news_type?

    #1135987

    Minesh
    Supporter

    Languages: English (English )

    Timezone: Asia/Kolkata (GMT+05:30)

    Hello. Thank you for contacting the Toolset support.

    Well - the code you shared is a custom code where I do not see Toolset is applied. However - its hard to tell where exactly the issue with your custom code.

    If you can share problem URL and access details I will try to check once (please note that its custom code).

    I have set the next reply to private which means only you and I have access to it.

    #1136710

    Minesh
    Supporter

    Languages: English (English )

    Timezone: Asia/Kolkata (GMT+05:30)

    Well - I dont know but you maybe using wrong method to query the posts.

    If I use the following method, it works and I can see its showing correct filtered results:

     $wp_query = get_posts( $args ); 

    See this link:
    => hidden link

    I suggest you should consult WordPress expert here and check with them which method should be used here to query the posts.

    #1136962

    Minesh,

    Thanks for taking a look and pointing me in the right direction. I've gotten the search to work but I'm noticing that my query to build the attorneys drop down isn't working on the results page. Why would this be? It works fine on every other page except for the search page. My nav is fed via the /inc/top.php file.

    #1137514

    Hi,

    Minesh isn't available, he has one day vacation, you will get the response ASAP when he back on Thursday.

    #1138160

    Minesh
    Supporter

    Languages: English (English )

    Timezone: Asia/Kolkata (GMT+05:30)

    Well - I checked the inc/top.php file but the thing is that what you added there is completely custom queries and custom code which is unfortunately beyond the scope of our support policy.

    If you need custom programming help for your project, please feel free to contact our certified partners where you can consider hiring a professional from our list of recommended consultants:
    => https://toolset.com/contractors/