I'm working on a client site that has two different custom post types: artist
and portfolio
. I'm using the posts-to-posts plugin to create a relationship from each artist to their portfolios.
Artist URL's are setup as {siteUrl}/artist
, while portfolio URL's are setup to receive the artist name: {siteUrl}/artist/%artistname%
I wrote the following to check if the portfolio
is connected to an artist
, and if so, change the URL of that portfolio
to {siteUrl}/artist/artist-name/portfolio-name
. If it doesn't have an artist connected, it directs changes the URL to {siteUrl}/portfolio/portfolio-name
.
function filter_portfolio_link( $post_link, $post ) {
if ( $post->post_type === 'portfolio' ) {
$connected = new WP_query( array(
'post_type' => 'artist',
'connected_type' => 'portfolios_to_artists',
'connected_items' => $post,
'nopaging' => true,
) );
if ($connected->have_posts() ) {
foreach ( $connected as $connectedPost ) {
setup_postdata($connectedPost);
if ($connectedPost->post_type === 'artist') {
$artistName = $connectedPost->post_name;
$first = false;
}
}
$post_link = str_replace( '%artist_name%', $artistName, $post_link );
}
else {
$post_link = str_replace( 'artist/%artist_name%', 'portfolio', $post_link);
}
}
return $post_link;
}
add_filter( 'post_type_link', 'filter_portfolio_link', 10, 2);
This pulls the correct data and inserts it in the URL correctly, but with lots of PHP notices. I'm not so worried about those at the moment.
So while this changes the slug correctly, it isn't changing the permalink and I'm getting 404 errors on the front-end. I imagine this needs a rewrite function to be paired with it? Just not sure where to go from here.
This (Getting Post ID from slug during init) seemed to hit somewhere around what I'm looking for, though not quite exactly. Any ideas?