• Is there a way to output a count of the number of pages/posts that fit a certain filter? For example, the number of child pages of the current page? The number of comments on a specific page?

    For example, I’m making a kind of encyclopedia. There are topics which for the most part for now are countries. The child pages of each country have the information I display on the country, using ACF on each page to store the information. Each page can have comments. When listing the child pages on the main Country page, I want to show how many comments are on each child page as a kind of indication of how how discussion is taking place.

    Some countries have Provinces which are also child pages, and have their own child pages with information. These Province pages would be topics in their own right. In the future there may be some countries and provinces that have city child pages which would also be topics.

    I’d also like to have a widget on the front page of the site that lists the number of topics (countries, provinces, cities), the number of resources (all child pages of all topics), and all comments.

    Is this possible? Thanks.

    https://www.ads-software.com/plugins/custom-content-shortcode/

Viewing 14 replies - 16 through 29 (of 29 total)
  • Thread Starter philipt18

    (@philipt18)

    Using the new [loop-count] functionality, I inserted the following code:

    [pass list=A~Z]
    [loop exists type="page" parent=this field=alpha value={ITEM} orderby=name]
    <strong><span style="font-size: 130%;">{Item}</span></strong>
    <p></p>
    [the-loop]
    [field title-link] ([loop-count type=page parent=this include=children field=type value=resource,dependency])<br/ >
    [/the-loop]
    <p></p>
    [/loop]
    [/pass]

    and got an Internal Server Error. Any idea why?

    Plugin Author Eliot Akira

    (@miyarakira)

    Yes, this is related to the other thread: [loop-count] is performing a query loop inside [the-loop] and [loop exists], and confusing the current loop state. I think I can solve this by changing [loop exists] to protect its own state from inner loops – it’s tricky, so I’ll need some time to develop and test it.

    Thread Starter philipt18

    (@philipt18)

    Thanks.

    Plugin Author Eliot Akira

    (@miyarakira)

    As far as I tested, [loop] and [loop-count] are working fine inside [loop exists] and [the-loop]. So, the cause of the server error seems to be in the queries themselves, that it’s falling into an infinite loop somehow.

    This is a hard one to diagnose, because I’m not able to reproduce the issue. You might try different queries in [loop-count] to see which parameter triggers the error.

    Thread Starter philipt18

    (@philipt18)

    I tried several variations but all caused errors.

    Including simply this:

    ([loop-count type=page parent=this])

    Maybe it’s just taking up too much memory? I have 205 topics on the index page, and for each of them it would be running the loop-count to determine how many resource pages there are for that topic.

    Plugin Author Eliot Akira

    (@miyarakira)

    Interesting that such a simple query could cause a server error. You might be right about memory, there are many loops in there and each of them is multiplied – for example, for each of 205 topics, it’s probably going through all resource pages, every time.

    Some ideas:

    – You could try setting a limit to the number of posts on [loop exists], to see if a smaller number works. In that case, it’s likely a memory issue.

    – You could install Query Monitor to inspect how many queries are being run and how much memory it’s taking up. But..if you’re getting a server error, then you will not see the page at all..

    – You could increase the memory allocated for PHP. This depends on your hosting and server configuration. Here’s an article that might be helpful: 5 Ways to Increase the PHP Memory Limit in WordPress

    Thread Starter philipt18

    (@philipt18)

    So as a test I added an [if] statement to have it run the [loop-count] only on one topic:

    [pass list=A~Z]
    [loop exists type="page" parent=this field=alpha value={ITEM} orderby=name]
    <strong><span style="font-size: 130%;">{Item}</span></strong>
    <p></p>
    [the-loop]
    [field title-link][if name=france] ([loop-count type=page parent=this include=children field=type value=resource,dependency])[/if]<br/ >
    [/the-loop]
    <p></p>
    [/loop]
    [/pass]

    And it worked fine. I then tried it with united-states instead of france, as the US has over 50 child topics (50 states plus several territories) and it didn’t give an error, but it only returned the number of resources/dependencies that were children of united-states, and didn’t return results from the 50+ children (which doesn’t make sense to me since I have the include=children setting in place).

    Putting aside the descendent issue for a minute, considering that the [loop-count] itself isn’t throwing an error, I would think it does point to a memory issue. My question then is if there might be a better way of handling memory. Are you accumulating memory throughout the process? Is there a way to map out the hierarchy of queries that will be made and then run them in sequence separately so you’re not using too much memory at any given time?

    Actually, I can now confirm it’s a memory issue as I managed to increase the memory allocated to PHP from 64MB to 128MB and it worked (although it’s slow). That said, however, I still think it might be worth looking into how you manage memory and see if you can map out the queries in advance and not accumulate memory as you go. Now I need to see if I can figure out your caching system to speed up the display of this page (which doesn’t need to updated more than once a day).

    Plugin Author Eliot Akira

    (@miyarakira)

    For improving memory consumption, there’s not much I can do because of how PHP, WP_Query and shortcodes work – all post content processing happens in memory, and the loop must have the whole query to compile the result to return.

    Aside from allocating more memory or upgrading the server, I recommend either caching parts of the page that require heavy queries (as you mentioned already), or using a CDN to serve cached static pages.

    Thread Starter philipt18

    (@philipt18)

    I was thinking maybe it would be possible to look ahead, map out the queries, do some queries and keep the data in static arrays, and then feed that data to a new queries as needed. I’m not an expert on this, but I would think there’s a way to do a query in parts. Maybe not.

    Thread Starter philipt18

    (@philipt18)

    So I just upgraded to the new version, 3.6, and something odd happened. Whenever the result is supposed to be 0, it shows 1098. My current code is:

    [pass list=A~Z]
    [loop exists type="page" parent=this field=alpha value={ITEM} orderby=name]
    <strong><span style="font-size: 130%;">{Item}</span></strong>
    <p></p>
    [the-loop]
    [field title-link] ([loop-count type=page parent=this include=children field=type value=resource,dependency])<br/ >
    [/the-loop]
    <p></p>
    [/loop]
    [/pass]

    In addition, I still have the problem that the children of children are not showing up in the results. In other words, the main page (where the above code resides) has children which are countries (i.e. France, United States, etc.). Other than the Zero problem above, it properly shows the children of those countries which are Resources/Dependencies (child pages of the Topics which have the field ‘type’ set to Resource or Dependency). However, in the case of countries that have children that are also Topics (i.e. the United States has child Topics for each state and territory), it doesn’t show the Resources and Dependencies that are the children of those child-topics (i.e. States). In the case of the US I have only 7 resources listed for the country, but each state has at least one resource (and several have many) so at the very least the result should be 57, but instead I’m just getting 7 as the loop-count for the United States. Any idea why that is?

    Thanks.

    Thread Starter philipt18

    (@philipt18)

    1098 might be the total number of pages whose type filed is either resource or dependency on the entire site. I’m not certain, but it seems approximately how many there are.

    That doesn’t really make sense though since it’s on an inside loop-count where the parent whose children it is counting has no children with those type field is set to resource or dependency (by definition since these are the ones that normally show 0).

    So I really have no idea what is going on.

    Thread Starter philipt18

    (@philipt18)

    Putting aside the 1098 issue for a moment (it does seem to be linked to the total number of resources by the way since I added more and the number went up accordingly to 1223), I think I have a good way to drop the memory requirements significantly, although it’s based on something I’m not sure I can do.

    Is there a way to write to a custom field? I couldn’t figure out how to do that in the documentation.

    Here’s my idea. I create a new custom field that will hold the count of resources and dependencies for that page. Whenever the page is generated (such as for a country or a region) it uses the math module to add up the number of resources and dependencies for that page, and at the end, writes the total to the custom field for that page.

    On the main page where I run into all these memory issues, instead of having to run the loops on each page, I now just add up the numbers in those custom fields. It seems that would be much faster and use much less memory, although it relies on the ability to write to a custom field, and I don’t know how to do that.

    Thread Starter philipt18

    (@philipt18)

    Any idea about the 1098 issue? Why doesn’t it display 0? And is it possible to write a value to a custom field? Thanks.

    Thread Starter philipt18

    (@philipt18)

    Eliot, any update on why loop-count never returns, but a large number instead?

    I’ll move my writing to a custom field to a separate thread…

Viewing 14 replies - 16 through 29 (of 29 total)
  • The topic ‘Counting pages/posts’ is closed to new replies.