'Remove action on specific page [duplicate]

I have function to filter post in Ajax:

function load_scripts() {

    wp_enqueue_script('ajax', get_template_directory_uri() . 'scripts.js', array('jquery'), NULL, true);

    wp_localize_script('ajax' , 'wp_ajax',
        array('ajax_url' => admin_url('admin-ajax.php'))
        );

}

add_action( 'wp_enqueue_scripts', 'load_scripts');

add_action( 'wp_ajax_nopriv_filter', 'filter_ajax' );
add_action( 'wp_ajax_filter', 'filter_ajax' );

function filter_ajax($taxonomy) {


$category = $_POST['category'];


$args = array(
  'post_type' => 'post',
  'posts_per_page' => 50,
  'category__in' => 6
);


if(isset($category)) {
  
    $args['category__in'] = array($category);
}


        $query = new WP_Query($args);

        if($query->have_posts()) :
            while($query->have_posts()) : $query->the_post();
                the_title('<h2>', '</h2>');
                the_content('<p>', '</p>');
            endwhile;
        endif;
            wp_reset_postdata(); 


    die();
}

I want to remove_action in specific page (faq). I trying with this:

  function faq_disable() {
      if( is_page('faq')) {
        remove_action( 'wp_ajax_nopriv_filter', 'filter_ajax' );
        remove_action( 'wp_ajax_filter', 'filter_ajax' );
      } else {
          echo "Not working";
      }
    }
    add_action( 'wp_head', 'faq_disable' );

but that is not working. What am I doing wrong?

Edited//

FAQ.php

<div class="js-filter">
    <?php
    $args = array(
    'post_type' => 'post',
    'posts_per_page' => 100,
    'child_of' => 10,
    'exclude' => array(1),
    );

    $query = new WP_Query('cat=10');

    if($query->have_posts()) :
        while($query->have_posts()) : $query->the_post();
            the_title('<h2>', '</h2>');
            the_content('<p>', '</p>');
        endwhile;
        endif;
    wp_reset_postdata(); ?>
</div>
<div class="categories">

<ul>
    <?php 
        $cat_args = array(
            'exclude' => array(1),
            'child_of' => 10
        );
        ?>

<?php 
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>

        <li class="js-filter-item"><a href="#">All</a></li>

        <?php $categories = get_categories($cat_args); foreach($categories as $cat) : ?>
        <li class="js-filter-item"><a data-category="<?= $cat->term_id; ?>" href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
    <?php endforeach; ?>
</ul>
</div>

products.php

<div class="js-filter">
    <?php
    
    $args = array(
    'post_type' => 'post',
    'posts_per_page' => 100,
    'child_of' => 6,
    'exclude' => array(1,10),
    );

    $query = new WP_Query('cat=6');

    if($query->have_posts()) :
        while($query->have_posts()) : $query->the_post();
            the_title('<h2>', '</h2>');
            the_content('<p>', '</p>');
        endwhile;
        endif;
    wp_reset_postdata(); ?>
</div>
<div class="categories">

<ul>
    <?php 
        $cat_args = array(
            'exclude' => array(1,10),
            'child_of' => 6
        );
        ?>

<?php 
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>

        <li class="js-filter-item"><a href="#">All</a></li>

        <?php $categories = get_categories($cat_args); foreach($categories as $cat) : ?>
        <li class="js-filter-item"><a data-category="<?= $cat->term_id; ?>" href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
    <?php endforeach; ?>
</ul>
</div>

js

  (function($){
    $(document).ready(function(){
        $(document).on('click', '.js-filter-item > a', function(e){
            e.preventDefault();

            var category = $(this).data('category');

            $.ajax({
                url:wp_ajax.ajax_url,
                data: { action: 'filter', category: category },
                type: 'post',
                success: function(result) {
                    $('.js-filter').html(result);
                },
                error: function(result) {
                    console.warn(result);
                }
            });
        });
    });
})(jQuery);


Solution 1:[1]

Give this a shot. As you could apply the same logic to the ajax filter.

function load_scripts() {
    if( is_page('faq')) return; // Check for page prior to even enqueuing
    wp_enqueue_script('ajax', get_template_directory_uri() . 'scripts.js', array('jquery'), NULL, true);

    wp_localize_script('ajax' , 'wp_ajax',
        array('ajax_url' => admin_url('admin-ajax.php'))
        );

}

add_action( 'wp_enqueue_scripts', 'load_scripts');

add_action( 'wp_ajax_nopriv_filter', 'filter_ajax' );
add_action( 'wp_ajax_filter', 'filter_ajax' );

function filter_ajax($taxonomy) {


$category = $_POST['category'];
    
    if (is_page('faq') ) {
        $cat__in = 10;
    } else {
        $cat__in = 6;
    }

$args = array(
  'post_type' => 'post',
  'posts_per_page' => 50,
  'category__in' => $cat__in,
);

if(isset($category)) {
  
    $args['category__in'] = array($category);
}


        $query = new WP_Query($args);

        if($query->have_posts()) :
            while($query->have_posts()) : $query->the_post();
                the_title('<h2>', '</h2>');
                the_content('<p>', '</p>');
            endwhile;
        endif;
            wp_reset_postdata(); 


    die();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1