Support

Account

Home Forums General Issues Custom Post Type Relationship Archive Page

Solving

Custom Post Type Relationship Archive Page

  • Hey,

    I’ve got two different custom post types, Works & Artists. I’m using the Relationship field to link Work posts to Artists, and Artists to Works.

    My URLS are:

    site.com/works/
    site.com/artists/

    On a single Artist post i’m teasing four Work posts then I have a view all works button which I then want to send them to a listings page showing all the works relating to that artist.

    I would also like the URL to be site.com/artists/james-smith/works/ but keep the single work posts url to site.com/works/xx

    How would I go about doing something like this ?

    Any help would be greatly appreciated <3

  • /**
     * URL Rewrite
     */
    add_action( 'generate_rewrite_rules', 'custom_rewrite_rules' );
    function custom_rewrite_rules()
    {
    	// get global rules
    	global $wp_rewrite; 
    
    	// add custom rules
    	$new_rules = array(
    		"artists/([^/]+)/works/?" => "index.php?post_type=artists&post_name=" . $wp_rewrite->preg_index(1)
    	);
    
    	// put custom rules into global rules
    	// or use array_push() ... 
    	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    // go to settings -> permalinks to flush your rules

    Check out generate_rewrite_rules here … btw its not an issue from ACF!

  • Thanks @pixelbart – that worked in rewriting my urls. My next question would be around what I would label my template. With your change it’s now loading my Artists landing page when i goto site.com/artists/joe/works.

    Also is there a special query I would need on that page to pull the content through ?

    Apologies for posting in the wrong forum, I wasn’t sure which to put it in.

    Thanks again for your help!

  • You can access your content within the loop via get_field ('field_slug');. So when you get your “relationship” via get_field (), you have access to the content.

    The loop should look like this:

    // check if entries exists
    if( have_posts() ):
    
    // loop start
    while( have_posts() ): the_post();
    
    // YOUR FIELD
    
     // loop end
    endwhile;
    
    // reset query
    wp_reset_postdata();
    
    // end
    endif;

    You can check the contents of the field with print_r( get_field('field_slug') );.

    Some Links:
    get_field
    wp_query

  • Sorry for the late reply:

    When I put this code in my archive page for artists, it only outputs the field data for the latest post in that custom post type, rather than the one in the url.

    Eg: Three artists in order:

    Joe
    James
    Jack

    If I’m on site.com/artists/jack it out puts the data for Joe.

    <div class="container-fluid">
    <?php // check if entries exists
    if( have_posts() ):
    
    // loop start
    while( have_posts() ): the_post(); ?>
    
        <?php 
    
    $posts = get_field('featured_works');
    
    if( $posts ): ?>
    	<ul>
    	<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
    	    <li>
    	    	<a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
    	    	<span>Custom field from $post: <?php echo $post->post_name; ?></span>
    	    </li>
    	<?php endforeach; ?>
    	</ul>
    <?php endif; ?>
        
        
    <?php endwhile;
    
    // reset query
    wp_reset_postdata();
    
    // end
    endif; ?></div>
  • Is that correct? <?php echo $post->post_name; ?> $post? 🙂

    And what is the name of the file? Is it an archive-CPT_SLUG.php file?

  • Sorry, i’m not sure I follow?

    The name of the file is archive-artists.php

  • Then your query is correct. But if you want to query individual artists, you have to use a single-artists.php.

    Another tip: If you name your variable $works and not $posts then it’s easier to read:

    <?php 
    
    $works = get_field( 'featured_works' );
    
    if( $works ):
    
    foreach( $works as $work ):
    
    echo get_the_permalink( $work->ID );
    
    endforeach;
    
    else:
    
    echo sprintf( '<p>%s</p>', __('No work here...') );
    
    endif;

    In your field ‘featured_works’ you have an object with work?

  • Hmmm, not having any luck. There’s definitely something in there. However the data that’s still being displayed is coming from the latest artist and not the one that appears in the URL.

    Is there a way of querying what data is displayed based on the artist post slug ? (site.com/artists/jack/works)

    Sorry i’m completely stumped with this and everything I try just isn’t working.

  • Check out global $wp_query; and than print_r( $wp_query );… than you get the global object.

    You also can use query vars and something like this:

    <?php
    
    $arguments = array(
      'post_type' => 'artists',
      'name' => get_query_var( 'name' )
    );
    
    $artists = new WP_Query( $arguments );
    
    if( $artists->have_posts() ):
    
    while( $artists->have_posts() ): $artists->the_post();
    
    // your code
    
    endwhile; wp_reset_postdata();
    
    endif;
Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Custom Post Type Relationship Archive Page’ is closed to new replies.