Skip Navigation

[Resolved] Setting Archives Widget for CPT

This support ticket is created 5 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by pedro.S 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1193632

Tell us what you are trying to do? Setting Archive Widget for CPT

Is there any documentation that you are following?
https://stackoverflow.com/questions/44966821/wordpress-custom-post-type-archive-widget-to-display-only-cpt
https://stackoverflow.com/questions/14486792/custom-post-type-yearly-monthly-archive

I already installed this plugin (hidden link) but it doesn't show any results.
I don't want to mess with the Archive default widget since it's beeing used in the Blog Area of the site.

What i want is to create a widget (or a html shortcode widget) that returns the custom post types of type "publications" by Year/month.

So it would be:

Year 1
Month (12)
Month (21)
Year 2
Month (3)
Month (5)
etc.

How can this be achievable?

#1193679

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You want to modify the standard WordPress archive widget so that instead of displaying standard posts it displays the count of a specific custom post type.

Toolset can't help you here, you'll need to do some coding that modifies the query which retrieves the posts, but that is only half the story, because the widget links to the month archives, and those will also only display standard WordPress posts, so you will also need to modify the queries for such archives to also display your custom posts.

There is a good explanation here: https://wordpress.stackexchange.com/a/154750/35739.

Note that the code samples in that post are to include a custom post type *as well as* standard posts, you will want to modify them to only include your standard posts.

#1193752

Thank you for your time Nigel!

Actually i would like to separate both, WordPress Archives / Custom Toolset Types.
So basically it would be 2 types of archives.

This code:
add_filter( 'getarchives_where', function ( $where )
{
$where = str_replace( "post_type = 'publicacoes'", "post_type IN ( 'post', 'publicacoes' )", $where );
return $where;
});

add_action( 'pre_get_posts', function ( $query )
{
if ( !is_admin()
&& $query->is_main_query()
&& $query->is_archive()
)
$query->set( 'post_type', array( 'post', 'publicacoes' ) );
});

Works. But it mixes both content, the wordpress post and the CPT Publicacoes posts on the same archive.
The ideal would be to create a shortcode that i could insert in a text widget, so i could create a widget specifically for the CPT Archive.

#1193769

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Monthly archives are built-in to WordPress.

You can customise what is shown at the monthly archive (using pre_get_posts, as per your code), but there is only one archive, which is displayed at the corresponding URL. So it could show posts, or custom posts, or both posts and custom posts, but you can't have one for posts and another for custom posts.

You could create separate widgets for posts and for custom posts, which showed the counts for that post type against each month, but the link in each case would lead to the one monthly archive, which would need to display both post types if it were to work with both widgets.

You could make an alternative widget that linked to a static page where you inserted a View with a date filter so that it only showed the custom posts for the month that was passed in the link from the widget. But you cannot easily reproduce the structure of the standard WordPress archive widget (grouping months by year and displaying counts), you would effectively need to write your own custom widget for that (using the standard archive widget as a starting point and customising it).

#1193786

Thank you Nigel!

I see what you mean. I will dig more into it.