functions.php 的问题

Problems with functions.php

function getParentID() {
    $url = 'http://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
    $pageID = url_to_postid($url);
    $pageID = trim($pageID);
    return $pageID;
}

function OrderFieldsQuery($args) {

    //->> WORKS WITH HARD-CODED ID'S
    $args['post_parent'] = 52;

    $args['post_parent'] = getParentID();
    // Doesn't work in that way, even function returns same value;

    return $args;
}

add_filter('acf/fields/relationship/query', 'OrderFieldsQuery', 10, 1); // add key to filter

**附加示例: **

 global $wp;
    $currentUrl = home_url(add_query_arg(array(),$wp->request));
    $ID =  url_to_postid($currentUrl);
    $args['post_parent'] = $ID;
    $args['post_parent1'] = 52;
    if ($args['post_parent'] === $args['post_parent1'])
        $args['equal'] = true;
    // Doesn't work in that way, even function returns same value;

    return $args;

我得到的输出…………:

数组 ( [post_parent] => 52 [post_parent1] => 52 [等于] => 1 )

所以它们是相等的,但只有在硬编码时才有效。

试试这个

function OrderFieldsQuery($args) {

    //->> WORKS WITH HARD-CODED ID'S
    $args['post_parent'] = 52;

    $url = 'http://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
    $pageID = url_to_postid($url);
    $pageID = trim($pageID);
    $args['post_parent'] = $pageID;
    // Doesn't work in that way, even function returns same value;

    return $args;
}

add_filter('acf/fields/relationship/query', 'OrderFieldsQuery', 10, 1); // add key to filter

从下面的代码

获取页面url
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));

现在使用下面的代码获取页面 ID

  $postid = url_to_postid( $current_url );

现在您将获得 id