$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom post types - Search result based on URL|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - Search result based on URL

matteradmin9PV0评论

Currently I am using bellow function to display search results from Custom Post Type.

add_filter( 'pre_get_posts', 'tgm_io_cpt_search' );
function tgm_io_cpt_search( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array( 'lp_course', 'lp_lesson', 'products', 'portfolio' ) );
    }
    return $query;
}

I need something more advanced. I would like to set post_type based on referral parameter.

Lets say if customer is searching from http://mywebsite/course, then above function should change and return queries for curse only:

$query->set( 'post_type', array( 'lp_course' ) );

I tried to do it via HTTP_REFERER, but was not successful.

$host =  $_SERVER["HTTP_REFERER"];

if ( $host == 'https://mywebsite/course' ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array( 'lp_course') );
    }
} else {
    if ( $query->is_search ) {
        $query->set( 'post_type', array( 'lp_course', 'lp_lesson', 'products', 'portfolio' ) );
    }
    return $query;
}

Currently I am using bellow function to display search results from Custom Post Type.

add_filter( 'pre_get_posts', 'tgm_io_cpt_search' );
function tgm_io_cpt_search( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array( 'lp_course', 'lp_lesson', 'products', 'portfolio' ) );
    }
    return $query;
}

I need something more advanced. I would like to set post_type based on referral parameter.

Lets say if customer is searching from http://mywebsite/course, then above function should change and return queries for curse only:

$query->set( 'post_type', array( 'lp_course' ) );

I tried to do it via HTTP_REFERER, but was not successful.

$host =  $_SERVER["HTTP_REFERER"];

if ( $host == 'https://mywebsite/course' ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array( 'lp_course') );
    }
} else {
    if ( $query->is_search ) {
        $query->set( 'post_type', array( 'lp_course', 'lp_lesson', 'products', 'portfolio' ) );
    }
    return $query;
}
Share Improve this question edited Jan 22, 2019 at 21:02 Kashif Rafique 2351 gold badge3 silver badges12 bronze badges asked Jan 22, 2019 at 14:20 Back OfficeBack Office 1 1
  • Note that this would be incompatible with full page caching as the same URL would provide different results based on where the user had come from. btw did you know that you can just append the variables to the URL? e.g. tomjn/talks/?s=right searches my talks post type for the word right, as does /?s=right&post_type=tomjn_talks, using hidden inputs you can sneak plenty of extra parameters into a search form – Tom J Nowell Commented Jan 22, 2019 at 14:41
Add a comment  | 

1 Answer 1

Reset to default 0

You don't and shouldn't rely on referrers ( they can be stripped, hidden, or just outright lies )

Instead there are alternatives

such as submitting to the post type archive:

<form action="/courses" method="get">
    <input type="test" name="s"/>
</form>

Or bundling query vars as hidden inputs:

<form action="" method="get">
    <input type="test" name="s"/>
    <input type="hidden" name="post_type" value="course" />
</form>

Or even just:

mywebsite/?s=foo&post_type=course

etc

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far