2017-09-09 31 views
1
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 

**附加例如: **問題用的functions.php

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; 

輸出I獲得有關此.........:

陣列([post_parent] => 52 [post_parent1] => 52 [equal] => 1)

所以它們是相等的,但它只在硬編碼時才起作用。

+0

'var_dump(getParentID())'返回什麼? – Qirel

回答

0

試試這個

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 
+0

它返回正確的數組,但是當它進入add_filter('acf .....')時,它在那裏無法正常工作。 im試圖完成的任務是 - >爲頁面的acf搜索字段設置過濾器,所以我只看到具有相同ID(編輯頁面ID)的頁面。 IM ON現在就是哪個頁面的子項目。 functions.php - >嘗試獲取當前頁面ID,然後傳遞給add_filter。 再次。與硬編碼= 52 - >正常工作。 使用您的代碼示例NO,即使我在前端打印了與硬編碼相同的值。 也許它與類型有關。 Like URL == string ???? – revengeance

0

獲取頁面的url從下面的代碼現在

global $wp; 
$current_url = home_url(add_query_arg(array(),$wp->request)); 

使用下面的代碼來獲取頁面ID

$postid = url_to_postid($current_url); 

現在,您將獲得ID