• titi5895

    (@titi5895)


    Hi,

    I try to use url_to_postid() on my network but the function always returns 0 (failure).

    I know it works only for current blog urls that’s why I use switch_to_blog ().

    switch_to_blog ($blog);
    
    $id = url_to_postid($url);
    
    restore_current_blog();

    But nothing changes…

    Thanks in advance for your help

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    More information is needed.

    Where are you running this call? Are you already ON the site? Where did you define $blog?

    What is the FULL purpose of this code? Maybe we know something that’s already been done so you don’t have to reinvent the wheel.

    Thread Starter titi5895

    (@titi5895)

    Hi Mika,

    My Network is a multilingual multisite-installation.

    In header.php I call the get_additional_flags_trad () function of functions.php file.

    This function allows me to add an additional flag to my flags list already displayed.

    During the display of these flags as soon as I arrive at the Canadian flag (which refers to the English translation), I called get_additional_flags_trad () to display another Canadian flag which refers to the French translation.

    To display the French translation url, I have to know the English translation id.

    My problem is that, I don’t have this id (En) I only have url
    So I wish to use url_to_postid ().

    My Canadian blog is the blog # 18, when I display Canadian flags I’m not on this blog (18).

    That’s why I try to use the url_to_postid () preceded by a switch_to_blog (18);

    $blog is defined by a switch :

    switch ($sous_dom_link)
    {
      case "be": $blog ='3'; break;
      case "ch": $blog ='4'; break;
      case "ca": $blog ='18'; break;
    }
    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    get_additional_flags_trad isn’t a part of WordPress core.

    I’m sorry but this is something WAY too complex to try to debug with the info we have.

    Thread Starter titi5895

    (@titi5895)

    ok thanks anyway Mika

    came across the same issue while working on some functionality

    If this may help someone

    //convert the url to make sense to the target blog
    
    global $current_blog;
    
    $target_blog_details = get_blog_details($blog);
    
    $url = str_replace($current_blog->path, $target_blog_details->path, $url);
    
    //now the url will make sense to the target blog
    
    switch_to_blog ($blog);
    
    $id = url_to_postid($url);
    
    restore_current_blog();
    
    //this will work, provided that the translated url does have a corresponding post in the target blog
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘url_to_postid() with multisite’ is closed to new replies.