• Resolved wpusr007

    (@wpusr007)


    Hello,

    I am trying to retrieve the number of Pods posts in a certain pods with a specific “meta_value”. The “where” field of the Pods block “list items” works perfectly and I am trying to reproduce a similar result via PHP to only retrieve the number of posts BUT not actually display them…

    The following php block gives the number of posts in the “book” pod:

    <?php
    $count_posts = wp_count_posts( 'book' )->publish;
    echo $count_posts;
    ?>

    However, if I want to only get the number of posts where the “genre” field is set to value “Fiction”, I tried this code:

    <?php
        $posts = get_posts(array(
            'post_type' => 'book',
            'meta_key' => 'genres',
            'meta_value' => 'Fiction'
        ));
    echo count($posts);
    ?>

    However WP is returning “5” when it should return much more…. This seems to be normal as “get_posts” is apparently “coded” to return 5 unless some options are set. I tried to add:

    'posts_per_page' => -1,

    and

    'nopaging' => true,

    but WP is returning:

    Parse error: syntax error, unexpected ''nopaging'' (T_CONSTANT_ENCAPSED_STRING), expecting ')' in /mnt/data/wordpress-data/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(97) : eval()'d code on line 6

    What am I missing here?

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

    (@keraweb)

    Hi @wpusr007

    I’m actually unfamiliar with the nopaging param, however, if you use the posts_per_page param then that should be enough! You can just omit the nopaging param and it should work!

    Cheers, Jory

    Thread Starter wpusr007

    (@wpusr007)

    Hey Jory,

    So you’re right. The 'posts_per_page' => -1, is sufficient to do the job, but it must be entered before the meta_value line otherwise I get

    Parse error: syntax error, unexpected ''posts_per_page'' (T_CONSTANT_ENCAPSED_STRING), expecting ')' in /mnt/data/wordpress-data/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(97) : eval()'d code on line 6

    So the code order matters…. Good to know! now its working as expected!

    Thanks again!

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @wpusr007

    The order shouldn’t matter, I think this is related to a different issue.
    Maybe a forgotten comma or another markup issue?

    I also see that you are using a shortcode plugin, keep in mind that the input of PHP code within WordPress is a security risk.

    Cheers, Jory

    Thread Starter wpusr007

    (@wpusr007)

    Jory, yes it was a missing comma…. Thanks for pointing it out! Is it possible to “loop” this short snippet for each value in the pods relationship field “genres”? Right now, I tricked the code to use the page title (which corresponds to the genres) as the meta_value for the query, so this snippet can be reused on each different pages (as long as their titles matches what’s in the Pods genre field!):

    <?php
        $title = html_entity_decode( get_the_title() );
        $posts = get_posts(array(
            'post_type' => 'book',
            'meta_key' => 'genres',
            'posts_per_page' => -1,
            'meta_value' => $title
        ));
    echo count($posts);
    ?>

    It works well, but I’d like to output a small table like this:

    Fiction: 25
    Sci-Fi: 29
    Sciences: 15
    Politics: 35

    So basically I’d like to use an array for meta_value instead of a “static” value (like the page title). Retrieving the contents of the “genre” relationship field of the “book” pods has proven more complicated than I expected. Pretty much everything I tried hasn’t worked yet. Is it possible?

    For the php shortcode plugin, I understand but I could not get a way to execute PHP code inside of a page… This WP site is offline (only used on my LAN for personal/family usage).

    Looking forward to your reply! Have a great day and sorry for being to WP primitive ??

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @wpusr007

    There are many ways to work with relationships which are more stable.
    Please see our relationship fields or watch our introduction video for more information:

    https://docs.pods.io/videos/grow-beyond-posts-pages-introduction-pods-framework/
    https://docs.pods.io/fields/relationship/

    Cheers, Jory`

    Thread Starter wpusr007

    (@wpusr007)

    Excellent, I will go through the training material and post back if something is not working!

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Count pod entries in a specific pod (by meta_value)’ is closed to new replies.