0

На сайте использую ACF Пытаюсь вытащить посты с помощью Ajax но не выводит посты а выводит ошибку:

Fatal error: Uncaught Error: Call to undefined function get_query_var() in /opt/lampp/htdocs/baltik/wp-content/themes/baltik/request_template_page_catalog.php:17 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/baltik/wp-content/themes/baltik/request_template_page_catalog.php on line 17

Код файлика который дергаю аяксом request_template_page_catalog.php:

<?php
$filter_value_price_min = "300";
$filter_value_price_max = "700";

$filter_value_brand = "Jungheinrich";

$filter_value_year_min = "2008";
$filter_value_year_max = "2010";

$filter_value_operating_time_min = "25000";
$filter_value_operating_time_max = "25000";
?>
<?php
    $args = array(
        'post_type' => 'post-type-0',
        'publish' => true,
        'paged' => get_query_var('paged'),
    );

    query_posts($args);
?>
<?php if (have_posts()) :
    ?>
    <?php
    while (have_posts()) :
        the_post();
        ?>
        <?php
            $product_price_usd = get_field('acf_product_price')['acf_product_price_usd'];
            $product_code = get_field('acf_product_code');
            $product_img = get_field('acf_product_img');
            $product_year_of_issue = get_field('acf_product_year_of_issue');
            $product_capacity = get_field('acf_product_lifting_capacity');
            $product_fuel_type = get_field('acf_product_fuel_type');
            $product_brand = get_field('acf_product_brand');
            $product_operating_time = get_field('acf_product_operating_time');
        ?>
        <?php
        if (
            // Price
            $product_price_usd >= $filter_value_price_min &&
            $product_price_usd <= $filter_value_price_max &&

            // Brand
            $filter_value_brand == $product_brand or $filter_value_brand = "" &&

            // Year of issue
            $product_year_of_issue >= $filter_value_year_min &&
            $product_year_of_issue <= $filter_value_year_max &&

            // Operating Time
            $product_operating_time >= $filter_value_operating_time_min &&
            $product_operating_time <= $filter_value_operating_time_max
        )
        { ?>
            <div class="col-2">
                <div class="item card p-3">
                    <p><span class="badge badge-info">Title</span> <span
                            class="badge badge-success"><?php the_title(); ?></span></p>
                    <?php if (function_exists('add_theme_support')) the_post_thumbnail(array(0, 9999), array('class' => 'img-fluid')); ?>
                    <p><span class="badge badge-info">Capacity</span> <span
                            class="badge badge-success"><?php echo $product_capacity; ?></span></p>
                    <p><span class="badge badge-info">Product code</span> <span
                            class="badge badge-success"><?php echo $product_code; ?></span></p>
                    <a href="<?php the_permalink(); ?>" class="btn btn-success">Link</a>
                </div>
            </div>
        <?php } ?>
    <?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
<?php else : ?>
<?php endif; ?>
<?php wp_reset_query(); ?>

Код JavaScript

// Ajax
    let ajaxFilterFormCatalog = $('form#ajaxFilterFormCatalog');
    ajaxFilterFormCatalog.submit(function() {
        const data = new FormData();
        data.append('filter_value_price_min', $(this).find('input[name="filter_value_price_min"]').val());
        data.append('filter_value_price_max', $(this).find('input[name="filter_value_price_max"]').val());

        data.append('filter_value_brand', $(this).find('input[name="filter_value_brand"]').val());

        data.append('filter_value_year_min', $(this).find('input[name="filter_value_year_min"]').val());
        data.append('filter_value_year_max', $(this).find('input[name="filter_value_year_max"]').val());
        data.append('filter_value_operating_time_min', $(this).find('input[name="filter_value_operating_time_min"]').val());
        data.append('filter_value_operating_time_max', $(this).find('input[name="filter_value_operating_time_max"]').val());

        $.ajax({
            type: "POST",
            url: ajaxFilterFormCatalog.attr('data-url') + '/request_template_page_catalog.php',
            data: data,
            cache: false,
            contentType: false,
            processData: false,

            success: function(data) {
                // Выводим результат в консоль
                // console.log(data);
            }

        }).done(function(data) {
            $(this).find("input").val("");
            $("form").trigger("reset");
            console.log(data)
            $('#requestResponse').html(data)
        }).fail( function(data) {
            console.log(data);
            $('#requestResponse').html(data)
        });
        return false;
    })

Но если я этот код вставляю в Шаблон страници все работает

8
  • Всё неправильно, читайте выше. request_template_page_catalog.php - кто за вас ядро WP запустит-то? 16 дек 2020 в 15:22
  • @KAGG Design ну я так понимаю ему нужно указать что то типо // require_once('wp-config.php'); // require_once('wp-blog-header.php'); Правильно ? 16 дек 2020 в 15:34
  • Нет. Для загрузки ядра в вашем файле достаточно require_once 'путь\wp-load.php'. Но лучше всё делать правильно, так, как описано по ссылке выше. 16 дек 2020 в 15:51
  • Да но он не может найти этот файл Fatal error: require_once(): Failed opening required 'wp-load.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/baltik/wp-content/themes/baltik/request_template_page_catalog.php on line 4 Хотя этот файл находится в корне проекта, там-же где и wp-config 17 дек 2020 в 11:12

1 ответ 1

0

Нужно было просто добавить

require __DIR__ . '../../../../wp-load.php';

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.