K
K
Ksenia Timoshenko2022-04-13 16:10:20
WordPress
Ksenia Timoshenko, 2022-04-13 16:10:20

How to get page id in function.php file?

Hello. Tell me, is it possible to dynamically get the page id in the function.php file?

add_action('wp_ajax_nopriv_ajax_search', 'ajax_search');
add_action('wp_ajax_ajax_search', 'ajax_search');
function ajax_search()
{
    
    $postID = get_queried_object_id();
    $args = array(
        'post_type'      => 'lessonone', // Тип записи: post, page, кастомный тип записи
        'post_status'    => 'publish',
        'order'          => 'DESC',
        'orderby'        => 'date',
        'post_parent' => $postID,
        's'              => $_POST['term'],
        'posts_per_page' => -1
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();?>
            <li class="ajax-search__item">
                <a href="<?php the_permalink(); ?>" class="ajax-search__link"><?php the_title(); ?></a>
                <div class="ajax-search__excerpt"><?php the_excerpt(); ?></div>
            </li>
        <?php }
    } else { ?>
        <li class="ajax-search__item">
            <div class="ajax-search__not-found">Ничего не найдено</div>
        </li>
    <?php }
    exit;
}

The bottom line is that the search only searches for child pages of the current page

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question