简体   繁体   中英

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

**Additional example: **

 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;

Output i get on this.........:

Array ( [post_parent] => 52 [post_parent1] => 52 [equal] => 1 )

So they are equal but it works only when hard-coded.

Get page url from below code

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

Now use below code to get the page id

  $postid = url_to_postid( $current_url );

Now you will get the id

try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM