• Resolved jeremy2805

    (@jeremy2805)


    Hi,

    I have a custom post type where posts don’t have individual pages. A link to the post should take you to the appropriate archive page.

    It’s been pretty easy to do in general, but the search function returns posts and I want them to link to the appropriate archive page.

    Is there a recommended way to do it? I guess I can fiddle with the search template..?

    Many thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • ultimat1

    (@ultimat1)

    Could you provide a link so that I may fully understand what it is that you are trying to accomplish?

    Aashish

    (@aashishsonawane)

    Hi jeremy,

    For setting up redirection to respective archive page link, we can attach an action hook,

    I have tried to create a function for your solution but I have not tested it, you can modify the function as per your requirement.

    function search_filter($query) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_search && is_single('movie')) {
          $query->set('post_type_archive', array( 'movie_category' ) );
        }
      }
    }
    
    add_action('pre_get_posts','search_filter');

    In the above code:
    movie – custom post type
    movie_category – custom post type taxonomy

    Hope this solution would help, let me know if any more inputs are required from my end I will be happy to help.


    Regards!

    Thread Starter jeremy2805

    (@jeremy2805)

    Hi Aashish,

    Thanks for that. What I think you’re doing is adjusting the search query, whereas what I wanted to do was to change the behaviour of the search results. However, you got me thinking that I could stick a filter on the permalink function like so…

    function dino_link_permalink($url) {
    	global $post;
    
    	if ($post->post_type == 'simple_link') {
    		$dinoterms = wp_get_post_terms( $post->ID, 'simple_link_category' );
    
    		return esc_url( '/simple_link_category/' .$dinoterms[0]->slug. '/' );
    	} else {
    		return esc_url( get_permalink() );
    	}
    
    }
    add_filter('the_permalink', 'dino_link_permalink');

    Which works great, with no need to create a search template. Gotta love these filters.

    Many thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Set a posts URL to an archive page?’ is closed to new replies.