• Resolved mburdett555

    (@mburdett555)


    I am trying to load comments in a part of a page which is subject to page cache Disk. The rest of the page remains static, while comments can be posted and updated dynamically with each page load.

    Assuming the principal would be straightforward, I have used the below code:

    <!-- mfunc {hidden}-->
    $arr = get_comments(array("post_id" => get_the_ID(),"status" => "approve",));
    wp_list_comments( $args = array(), $arr);
    <!-- /mfunc {hidden}-->

    However get_the_ID() doesn’t run. After a page cache purge, yes it does – the first load. Thereafter, once the page is cached, get_the_ID() no longer works.

    Instead, I then tried the following code, adding a queried object fetch

    $obj = get_queried_object();
    $id = $obj->ID;
    $arr = get_comments(array("post_id" => $id,"status" => "approve",));

    Again, first load after page cache purge gets the right comments. Then subsequent reloads, it does nothing.

    The Problem: Every comment is loaded, with the status approve suggesting the post_id is completely ignored because its empty.

    Is there something I am missing from the page cache mfunc fragment?

    I’d rather use this than implement Ajax loading because I believe the comments to be beneficial on the page for SEO, and I may use them as a Q/A integrated into the ld+json schema.

    Many thanks. On the link, under the “Ask” tab. FYI those comments shouldn’t even load. They’re not under that franchise CPT’s ID.

    • This topic was modified 4 months, 4 weeks ago by mburdett555.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @mburdett555

    Thank you for reaching out and I am happy to help!
    “Page Fragment Cache” dynamically loads the content (in real-time) on your cached pages by parsing them for specific tags you manually embed.

    The comment/code blocks should be added to your theme template files as HTML and not within the PHP open (<?php) and close (?>) tags, as it will cause a PHP parse error/syntax error. Do not include any sensitive information in your code fragments; the code can be revealed if W3 Total Cache is deactivated, the Page Cache is changed to use an incompatible storage engine, or the defined?W3TC_DYNAMIC_SECURITY?string value is changed or not defined.

    Please check more details in our wiki page

    Thanks!

    Thread Starter mburdett555

    (@mburdett555)

    Sorry I think there has been a misunderstanding. I am following the FAQ guide, by doing this on a post page:

    <!-- mfunc -->
    echo time();
    echo "ID: ".get_queried_object()->ID;
    <!-- /mfunc -->

    The above prints out the epoch time as expected

    The above does not print out the queried_object_id, nor does get_the_ID() or get_queried_object_id()

    Therefore, I’m guessing its not possible to get the post ID from within a mfunc tag?

    Just to advise, I have closed the PHP tags before using the mfunc, so there is no syntax error

    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @mburdett555

    Thank you for your feedback.
    In this case, the best thin would be to use AJAX to bypass the cache entirely, as there are a lot of usecases that exist, and it’s imposible to without indepoth testing how it behaves.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.