• Resolved nmb52

    (@nmb52)


    I built a PODS structure of journal articles organized by issues, with articles classified by categories within each issue. So within an issue there may be editorials, reviews, essays, opinions, and so forth. This seems a straightforward structure that is a common use for PODS.

    I want to provide a dynamically created structured presentation of the articles in an issue, with the articles organized by category. I worked out a simple solution using an EACH approach that prints each article, but that results in an unstructured list of articles, which is not what I prefer. I want to print the issue title, then each category on a single line with the articles in that category clustered under it.

    I have a template that does more or less what I want. I use the template from a shortcode:

    [pods name="issue" where="slug='n17-2023'"  template="Issue Index"][/pods]

    The opening of the template is this; for the moment I must hardcode the slug value I want to display:

    <h2 style="margin: 0 0 0 0;"><a href="/{@slug}">{@name}</a></h2><br/>
    
    <?php
    $slug='n17-2023';

    What I want to do is pass the value of the slug given in the shortcode into a php variable, something like this:

    $slug="{@slug}";

    That does not work. In fact, digressing briefly, when set that way $slug has some very strange properties. The php code below gives the block of results a little farther down.

    $slug=(string)"{@slug}";
    
    echo "is_string slug: ".is_string($slug)."\n";
    print_r($slug);
    
    $slug1=substr($slug, 0);
    //$slug1 = print_r($slug, true);
    echo "is_string slug1: ".is_string($slug1)."\n";
    print_r($slug1);
    
    echo "\nslug:\n";
    for( $i=0; $i < strlen($slug); $i++ ){
      $j = substr($slug, $i, 1);
      echo "char $i: $j\n";
    }
    
    echo "\nslug1:\n";
    for( $i=0; $i < strlen($slug1); $i++ ){
      $j = substr($slug1, $i, 1);
      echo "char $i: $j\n";
    }

    The block below is what prints from the above code.

    is_string slug: 1
    n17-2023
    
    is_string slug1: 1
    n17-2023
    
    slug:
    char 0: {
    char 1: @
    char 2: s
    char 3: l
    char 4: u
    char 5: g
    char 6: }
    
    slug1:
    char 0: {
    char 1: @
    char 2: s
    char 3: l
    char 4: u
    char 5: g
    char 6: }

    print_r() sees $slug as the string ‘n17-2023’, which is what I want. Printed with substring() from 0 with no substring length parameter it sees it as ‘n17-2023’. Printed with substring() with a substring length, it has the value of ‘{@slug}’. I added a check to be sure that PHP sees it as a string, and it does. is_array($slug) returns false.

    Note also what happens with $slug1. I can set $slug1 either by substr($slug, 0) or by print_r($slug, true), and it shows as ‘n17-2023’. However, as did $slug, printed as individual characters by substring it has the value of ‘{@slug}’. So whatever is happening in $slug is transferable.

    It took me some time to find this; the value continued to print as ‘n17-2023’, but strlen($slug) insisted it had a length of 7, which turned out to be the length of ‘{@slug}’. When used as a string in an array of parameters to fetch the articles, it did not work. The first two are left over from testing before I isolated the weirdness with $slug. The uncommented line only works now because I hardcode $slug = ‘n17-2023’.

    $params = array(
      'limit' => -1,
    //  'where' => 'issue.slug = "{@slug}"',
    //  'where' => "issue.slug = '{@slug}'",
    //  'where' => "issue.slug = 'n17-2023'",
      'where' => 'issue.slug = "'.$slug.'"',
      'orderby' => 'arttype.ordine.meta_value'
    );
    $arts = pods( 'art' )->find( $params );

    I thought to add the strange properties of $slug as something that might interest you. I did not know PHP variables can have both wave and particle properties, depending on how one queries it.

    The real question is how might one pass a PODS tag into a PHP code block? I have searched in many ways and have looked through the documentation, but if I have seen how one can do that, it did not register. Can that be done? Thanks very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @nmb52

    It looks like you are trying to combine our magic tags with PHP. This won’t work. Magic tags can only be used in our Pods templates, not PHP.

    You should use PHP properties from the WP_Post or Pods objects that can be requested in PHP, few examples:

    Cheers, Jory

    Thread Starter nmb52

    (@nmb52)

    Excellent, thank you for your generous help. With that I was able to fetch the shortcode from post_content and regex isolate the issue identifier.

    What is curious is that “$slug=”{@slug}” almost worked. I have no idea why $slug acted so strangely when queried in different ways. I have speculated that it may have something to do with {…} fooling php into thinking it is working with an array, but I can’t make that hold up because the displayed contents don’t act like arrays, and is_array() returns false. Oh well… thanks again!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Pass a PODS tag/variable into PHP code’ is closed to new replies.