从自定义搜索表单中获取数据并在页面中显示结果(wordpress)

I am having an issue to display results from a search form in a page.

I have in my header.php a custom search form :

<form action="<?php echo site_url('/pagesearch/')?>" method="get" id="adminbarsearch">
    <input type="search" id="place"  name="mydata" />
    <input type="submit" class="adminbar-button" value="Rechercher"/>*/?>
</form>

When a user submits the form, I retrieve the data and modify it, through this code in my functions.php :

if ((isset($_GET['mydata'])) && (!empty($_GET['mydata']))){
    $q = sanitize_text_field($_GET['mydata']);      
    $search_posts_id = array();

    $search_args = array(
            's' => $q,
            'fields' => array('ids', 'post_type'),
            'post_type' => 'my_custom_posttype'
    );
    $search_query = new WP_Query($search_args);

    if ( $search_query->have_posts() ) :
            $search_posts_id = array_unique(wp_list_pluck( $search_query->posts, 'post_title', 'id' ));
            $search_posts_id = implode( ',', $search_posts_id );
    else :
            $search_posts_id = implode( ',', $search_posts_id );
    endif;
    wp_reset_postdata(); 

    echo $search_posts_id;

The problem is that I would like to display the result which is $search_posts_id in the "pagesearch" page of my website.

When I use the code above, it displays in the page but before the header and everything, and not in the content, between the header and the footer.

I don't know how to put this data correctly in the content.

Thank you very much for your help !

That's because you're echoing from your functions.php file -- this runs before the theme's header and content render, so it's going to just output before everything else.

You'll need to take a different approach, like potentially placing this code inside page.php (or whatever template you want it in) possibly before the the_content() call, or maybe even creating a custom shortcode to display this content wherever you want.

If you can provide some more info on where specifically you'd like these results to show up, we might be able to assist further. Good luck!