Skip to content
Advertisement

WordPress how to filter multiple custom post_type’s in a URL

In my WordPress v5.6, I am using the below code in Author page to show all current author’s posts from a custom post_type, song.

<?php
$author_page_link = esc_url(get_author_posts_url(false, ID->user_nicename));
?>
<a href="' . $author_page_link . '?post_type=song">All Songs</a>

The above link will filter all the songs from current author with a URL below:

http://www.music.com/author/current_author/?post_type=song

I want to show both “song” and “poem” custom posts from a author in the filter page.

I have tried below a links, with no luck:

<a href="' . $author_page_link . '?post_type=song+poem">All</a>
<a href="' . $author_page_link . '?post_type=song&post_type=poem">All</a>

How can I have a link to filter posts from multiple custom posts from a single author?

Update 1

I was using the archive-song.php template, that’s why only the song custom posts are showing in the page.

When using the below URL with ?post_type=song+poem, having archive.php only in template hierarchy, URL redirecting to author.php.

http://www.music.com/author/current_author/?post_type=song+poem

And if I use the below URL with ?post_type=song&post_type=poem, having archive.php only in template hierarchy, only the last post_type results are filtered (can be poem or song, whichever is the last query).

http://www.music.com/author/current_author/?post_type=song&post_type=poem

Update 2

This is my author page (https://pastebin.com/kCrYebcD). This page shows only the latest 10 posts from the current author of all post types.

This is my archive page (https://pastebin.com/twkWn5Bc). Here I want to show all the posts from the author from all the post types.

Advertisement

Answer

To include multiple Post Types via the URL parameter, you need to declare it as an array.

Your URL should look like this:

echo '<a href="' . $author_page_link . '?post_type[]=song&post_type[]=poem">All</a>';

This should work for you.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement