Using custom post types in custom page template
- markusParticipant3 years, 9 months ago #26833
Hi,
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?markusParticipant3 years, 9 months ago #26835Sorry, 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.
wpsolrKeymaster3 years, 9 months ago #26836Check out https://www.wpsolr.com/guide/actions-and-filters/search-results-modify-posts/
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, 9 months ago by wpsolr.
wpsolrKeymaster3 years, 9 months ago #26846Indeed, 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, 9 months ago by wpsolr.
You must be logged in to reply to this topic.