Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1308559
    hjaffer
    Participant

    Hi X

    I needed my users to search my site by post id and then automatically redirect them to the searched post. So I added the following to my functions.php

    
    /* 
    ===================
     Search By Post ID
    ===================
    */
    
    function search_id_pre_get_posts($query) {
    
        if($query->query_vars['s'] != '' && is_search()) {
            if(absint($query->query_vars['s'])) {
                $query->set('p', $query->query_vars['s']);
    
                $query->set('s', '');
            }
            }
            }
    
    add_filter('pre_get_posts', 'search_id_pre_get_posts');
    
    /* 
    ======================================
     Auto-redirect Post ID Search to Post
    ======================================
    */
    
    add_action( 'template_redirect', 'insta_single_result' );
    function insta_single_result() {
    
        if (is_search()) {
            global $wp_query;
    
            if ( 1 === $wp_query->post_count )
                wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
        }
    }

    That works perfectly to my liking.

    One thing remains that I’m a little struggling with. I want the above only executed on one page where I created in Cornerstone and placed search element in. I want it to be available ONLY to users going to that page (page id 4629). While search button is available to all users throughout the rest of the site, they shouldn’t have that functionality unless they landed on that page.

    Can you help me with this piece please?

    #1308588
    Christian
    Moderator

    Hey there,

    Regretfully this isn’t a feature offered by X. It could be possible with custom development, but this would be outside the scope of support we can offer. You might want to contact our trusted partners who caters X setup and customization needs. Please see https://community.theme.co/custom-development/. X is quite extensible with child themes, so there are plenty of possibilities. Thanks for understanding. Take care!

  • <script> jQuery(function($){ $("#no-reply-1308559 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>