Using custom post types in custom page template

  • markus
    3 years ago #26833


    I’m displaying posts from a custom post type contact on single page using a custom template for that page. I have blocked access to pages of that contact post type and therefore the only place where contacts are displayed is that specific one custom page.
    My question is whether there is a way to direct a user to that custom page when the user searches contacts?

    3 years ago #26834

    I’m not sure how it is related to WPSOLR?

    3 years ago #26835

    Sorry, I was a bit unclear.

    When a post (contact) is indexed by WPSOLR, the hierarchical post page is stored in the index as the url for that post. I would need to change the url so that a search result for the contact points to the page I created for contacts instead of the default post page.

    3 years ago #26836

    Check out

    Modify and copy the following code into your theme’s functions.php, or in a new small plugin.

    use wpsolr\core\classes\WPSOLR_Events;
    use wpsolr\core\classes\WpSolrSchema;
    add_action( 'after_setup_theme', function () {
        add_action( WPSOLR_Events::WPSOLR_ACTION_POSTS_RESULTS, 'wpsolr_action_posts_results', 10, 2 );
    } );
     * @param WPSOLR_Query $wpsolr_query
     * @param WPSOLR_AbstractResultsClient $results
    public function wpsolr_action_posts_results( WPSOLR_Query $wpsolr_query, WPSOLR_AbstractResultsClient $results) {
     // Modify urls of each post in $this->posts here
     // Just before it is returned to your theme for display
    • This reply was modified 3 years ago by wpsolr.
    3 years ago #26842

    Thank you for your response!
    That might be what I’m looking for. Though, I can’t see a way to change post’s permalink with that action. Is it also possible to change permalinks some way? $wpsolr_query includes post’s gui but no (pretty) permalink.

    3 years ago #26846

    Indeed, wpsolr_query->posts is an array of WP_Post.

    The theme’s search template is in charge of extracting the permalink with the_permalink() in the posts loop, which calls apply_filters( 'the_permalink', get_permalink( $post ), $post ).

    As an alternative, you could also catch the filter ‘the_permalink’ to return whatever you need:

    global $wp_query;
    if ( $wp_query instanceof WPSOLR_Query ) {
      // return static permalink when query is from WPSOLR
      return 'my_permalink';

    Or set a global variable in event WPSOLR_Events::WPSOLR_ACTION_POSTS_RESULTS’s code mentioned previously, and use the variable instead in the filter ‘the_permalink’.

    • This reply was modified 3 years ago by wpsolr.
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.