Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Nathan

Custom field that displays children count

Hi,

Firstly thanks for an excellent plugin. I’m building a website using wp-types.com’s toolset. I’d like to have an admin column that displays the amount of children in a column, so if a project has 30 documents it would display (30) in a column (preferably with a link to the document section filtered to that project name).

I’ve followed your instructions here about adding custom fields via the theme, but can’t get it to show. What would be the best way to display the children count?

Thanks

9 years, 2 months ago
Tobias Schutter
Developer

What types are these children; are they hierachical childs of these posts or are they stored in a custom field?

I will send you an example column plugin which you can modify to your needs.

9 years, 2 months ago
Nathan

Hi,

I’d say they are hierarchical childs, they’re all stored in wp_posts, is there a way I can check for you?

Thanks

9 years, 2 months ago
Tobias Schutter
Developer

I created a column starter-kit you can use as a starting point. Please download a copy here: https://github.com/codepress/cac-column-template. (Click the Download Zip button )

If they are hierarchical childs you should be able to retrieve them with a simple query, like this:


$children = get_posts( array(
       'post_per_page' => -1,
       'post_parent' => $post_id,
       'fields' => 'ids',
       'post_type' => 'page'
));

If you are using the starter-kit, place this in the get_raw_value and in the get_value you should then loop through them to add a link and title (and return them as a string).

9 years, 2 months ago
Tobias Schutter
Developer

Here is a fully working “Child Count” column created by using the starter-kit: cac-column-child_count.zip

9 years, 2 months ago
Nathan

Hi,

Thanks for that, looks very good! I installed it an all the child counts come up as zero. So perhaps they are stored in a different way – how can I provide you with the right info? I’m looking through the database now.

Thanks

9 years, 2 months ago
Tobias Schutter
Developer

It depends on how wp-types stores it’s data, you’ll have to look into their developer documentation or ask their support.

9 years, 2 months ago
Nathan

Hi,

I’ve sent them an email hopefully hear back soon. I was able to successfully display the parent name in a column with these settings.

9 years, 2 months ago
Tobias Schutter
Developer

Looking at your screenshot, they indeed do not use the post_parent, but a custom field called ‘_wpcf_belongs_project_id’.

Try to place this code in the get_raw_value() method in cac-column-child_count.zip. This will count all childs using the custom field.


$children = get_posts( array(
            'post_per_page' => -1,
            'fields' => 'ids',
            'post_type' => $this->get_post_type(),
            'meta_query' => array(
                array(
                    'key' => '_wpcf_belongs_project_id',
                    'value' => $post_id
                )
            )
        ));
return count( $children );
9 years, 2 months ago
Nathan

Hi,

Thanks for that! I tried that code but still returns the child count as zero. Types developers responded with this:

You can find children post ID of Custom Post Types created with Types querying the wp_postmeta table.

SELECT post_id FROM wp_postmeta WHERE meta_value = {$parent_post_id} AND meta_key = ‘_wpcf_belongs_{$parent_post_type_slug}_id’

Replace {$parent_post_id} with the parent post ID and {$parent_post_type_slug} with the parent CPT slug.

9 years, 2 months ago
Tobias Schutter
Developer

Great, you should be able to get the child post count with that query and display it in a column.

9 years, 2 months ago
Nathan

Thanks, I’ve been able to get it to return the number of children.

My structure goes like this:

– Projects
— Documents
— Containers
— Submissions

Ideally there would be a way of displaying & linking to those children. Click on the children count, the whole row drops down like an accordion and displays the children links and titles.

I’ll have a go at this tomorrow but do you think it’s achievable?

Thanks

9 years, 2 months ago
Tobias Schutter
Developer

You already retrieved the post_ids from the query. Now you should be able to loop through those an add a link and title. I would go for a simple slide toggle to display the children.

9 years, 2 months ago
Nathan

Okay, I’ve been able to render out everything correctly – I’d like to do it a bit neater than that with some external stylesheets and javascript.

I’ve played around a bit trying to include them in the right places but can’t seem to get it right.

How do I include a stylesheet and a script in the plugin, I only want it to load in pages where the child count column is used.

Thanks

9 years, 2 months ago
Tobias Schutter
Developer

You’re right about the scripts, it much nice to have seperate files for them. Our next release will have support for that too.

The syntax will be scripts(). Have a look at the cac-column-template on gitub.

If you like, I can send you a beta which has support for the new method.

9 years, 2 months ago
Nathan

Yes please, I need to keep moving on this.

9 years, 2 months ago
Nathan

I’ve also noticed that in the admin view it’s displaying EVERY item, and lagging out the whole interface as there is over 2000 items to load. Not sure why the pagination has stopped working. Any ideas?

9 years, 2 months ago
Nathan

If it go into admin columns, and reset the filtering for that type then pagination starts working. Sounds like a bug? Surely I can filter with pagination?

9 years, 2 months ago
Tobias Schutter
Developer

I just send you the beta.

The filtering issue is not something I heard before. Which column are you trying to filter?

9 years, 2 months ago

You must be logged in to reply to this topic.