Wordpress Custom Search by post_type

╄→尐↘猪︶ㄣ 提交于 2019-12-25 02:12:14

问题


I've tried a couple of methods but I cannot seem to filter custom post_types from my search results and was hoping someone could help.

I have installed "Job Manager" and created 4 jobs which have a custom post_type = 'jobman_job'

I tried to create a manual search form and set a hidden value of post_type = jobman_job but it still returned all posts.

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value=""/>
<input type="hidden" name="post_type" value="jobman_job" />
<input type="submit" id="searchsubmit" value="Search" />
</form>

I then tried creating a custom search page and redirecting the search to this page as follows (i.e added page_id hidden field):

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value=""/>
<input type="hidden" name="page_id" value="123" />
<input type="hidden" name="post_type" value="jobman_job" />
<input type="submit" id="searchsubmit" value="Search" />
</form>

And then in the custom search page, I added the following code (as per wordpress guide - http://codex.wordpress.org/Creating_a_Search_Page) and I added the post_type of jobman_job to the query array:

global $query_string;

$query_args = explode("&", $query_string);
$search_query = array('post_type' => 'jobman_job');

foreach($query_args as $key => $string) {
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$search = new WP_Query($search_query);

And it still displays all posts...

What am I doing wrong? I have checked the post_type column in the wp_posts table and I have 4 unique entries...so they are there...

Any Insight?


回答1:


As codex explains, after getting new data you need to replace the loop with your new data, like in this example

<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
 <?php setup_postdata($post); ?>

 <div class="post" id="post-<?php the_ID(); ?>">
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
   <?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
 </div>
 <?php endforeach; ?>
 <?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
 <?php endif; ?>

Displaying posts from custom query




回答2:


I simply left the html as is:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
  <input type="text" name="s" id="s" value=""/>
  <input type="hidden" name="post_type" value="jobman_job" />
  <input type="submit" id="searchsubmit" value="Search" />
</form>

and added the following to my functions.php

function mySearchFilter($query) {

    if (isset($_GET['post_type']) && $_GET['post_type'] == 'jobman_job') {
        $post_type = 'jobman_job';
    } else {
        $post_type = 'any';
    }
    if ($query->is_search) {
            $query->set('post_type', $post_type);
    };
    return $query;
};

add_filter('pre_get_posts','mySearchFilter');


来源:https://stackoverflow.com/questions/8177768/wordpress-custom-search-by-post-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!