在函数php中获取当前帖子ID,元查询筛选器

时间:2020-03-26 作者:Cory M

创建元查询过滤器时,如何在php函数中获取当前的帖子ID?我尝试了很多次,但都返回Null。当我手动输入一个数字时,过滤器按预期工作,但我需要它自动回显当前的帖子ID。感谢您的指导。

$args = array(
  \'post_type\' => \'project\',
  \'post_status\' => \'publish\',
  \'post_parent\' => \'HOW DO I GET CURRENT PAGE ID?\',
  \'meta_query\' => $meta_query,
); 
以下是完整功能:

add_action(\'wp_ajax_my_ajax_filter_search\', \'my_ajax_filter_search_callback\');
add_action(\'wp_ajax_nopriv_my_ajax_filter_search\', \'my_ajax_filter_search_callback\');

function my_ajax_filter_search_callback() {

  header("Content-Type: application/json");

  $meta_query = array(\'relation\' => \'AND\');

  if(isset($_GET[\'project_type\'])) {
    $project_type = sanitize_text_field( $_GET[\'project_type\'] );
    $meta_query[] = array(
        \'key\' => \'project_type\',
        \'value\' => $project_type,
        \'compare\' => \'=\'
    );
  }

  $args = array(
      \'post_type\' => \'project\',
        \'post_status\' => \'publish\',
        \'post_parent\' => \'HOW DO I GET CURRENT PAGE ID?\',
      \'meta_query\' => $meta_query,
  );

  if ( $search_query->have_posts()) {

      $filter_result = array();

      while ( $search_query->have_posts() ) {

          $search_query->the_post();

          $filter_result[] = array(
              "id" => get_the_ID(),
              "title" => get_the_title(),
              "permalink" => get_permalink(),
              "status" => get_field(\'status\'),
              "tags" => get_field(\'tags\'),
              "project_type" => get_field(\'project_type\'),
          );
      }
      wp_reset_query();

      echo json_encode($filter_result);

  } else {
    echo \'no content\';
  }
  wp_die();
}
这是页面上的节目

$ = jQuery;

var mafs = $("#my-ajax-filter-search"); 
var mafsForm = mafs.find("form"); 

mafsForm.submit(function(e){

    e.preventDefault(); 

    if(mafsForm.find("#project_type").val().length !== 0) {
        var project_type = mafsForm.find("#project_type").val();
    }             
    var data = {
    action : "my_ajax_filter_search",
    project_type : project_type
    }

    $.ajax({
      url : ajax_url,
      data : data,
      success : function(response) {
        mafs.find(".roadmap-search").empty();
        if(response) {
          for(var i = 0 ;  i < response.length ; i++) {
            var html += "<span class=\'meta category\'>" + response[i].project_type + "</span>";
            mafs.find(".roadmap-search").append(html);
          }
        } else {
          var html  = "No matching projects found. Try a different filter or search keyword";
          mafs.find(".roadmap-search").append(html);
        }
      } 
    });

});

1 个回复
SO网友:Tim Kozak

您只需通过ajax请求传递当前页面即可。

将此属性放在“body”标记上,以便将来能够使用JS获得此属性。

<body page-id="<?php get_the_ID(); ?>">
在JS中找到它

var page_id = $(\'body\').attr("page-id")
将其与其他“数据”一起传递:

var data = {
   action : "my_ajax_filter_search",
   project_type : project_type,
   page_id: page_id
}

相关推荐

如何输出所有的Apply_Filters和Do_action?

我想输出所有apply\\u过滤器和do\\u操作,以向用户提供它们的概述。如果我使用代码add_filter( \'wpErpOs_testFilter\', \'wpErpOs_testFilter_callback\', 10, 1 ); function wpErpOs_testFilter_callback( $testVar ) { $testVar[] = \"GM\"; return $testVar; } $testVar = array(\"V