• I have two custom post types registered.

    1. Custom Post Type: PUBLIKATIONEN with the two custom taxonomies LISTEN and AUTOREN
    2. Custom Post Type: AUTOREN

    Both have archives.

    With: <?php echo get_post_type_archive_link( 'autoren' ); ?> I can list all posts of the Custom Post Type AUTOREN (= writer’s profiles) on a PUBLIKATIONEN post. But what I want to do is to list only the posts of the writers who have contributed to a certain publication.

    Basically it’s like this

    Custom Post Type:
    PUBLIKATIONEN = Custom Post Type
    AUTOREN = Taxonomy
    Name = Term

    Custom Post Type:
    AUTOREN = Custom Post Type
    Name = Terms (no Taxonomy)

    So I need to fetch terms of the PUBLIKATIONEN custom post type and list them under the AUTOREN custom Post Type using a specific template (single-writers.php)

    basically what I need would be <?php echo get_post_type_archive_link( 'autoren' ); ?> with including only the terms of the specific post. But that doesn’t seem to be possible.

    Any ideas greatly appreciated!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t understand how the terms of a PUBLIKATIONEN post relate to the AUTOREN post type. Do AUTOREN posts have the same terms assigned?

    Without that understanding, all I can suggest is you review the possibilities of using the WP Query class to get the posts you want. If none of those arguments are suitable, you can query the database directly with $wpdb methods.

    Thread Starter gfaw

    (@gfaw)

    I’m pulling the AUTOREN (writers of a specific Publication) into the post with the following code:

    <?php
    echo '<ul class="publikationen-list-autoren">';
    echo get_the_term_list( $post->ID, 'autoren', '<li>', '</li><li>', '</li>' );
    echo '</ul>';
    ?>

    What I require is 2 things:
    1. I need to list all writer’s profiles assigned to a certain PUBLIKATIONEN post with a, I think, custom query that relates both entities but I don’t really know much about setting this up
    2. I need to configure a link which I can use with a button, so that when the button is clicked the user is taken to a listing page. Say the PUBLIKATIONEN post has 3 writers, the user will be taken to a page listing these 3 profile posts

    Though I believe that the slug of each the CPT AUTOREN term and the CPT PUBLIKATIONEN taxonomy AUTOREN term are equal and therefore the permalinks work.

    Moderator bcworkz

    (@bcworkz)

    This is sounding more like an unusual situation. I’m sure there is a solution, but I think it will take a bit more than a custom query. I should be able to suggest a solution, but first I need to fully understand your site’s configuration and what you are trying to do, so bear with me while I ask more questions, even though you’ve explained twice already.

    You say the taxonomy AUTOREN terms assigned to a PUBLIKATIONEN post should be the same as the CPT AUTOREN terms. Do you mean the AUTOREN post slug matches the taxonomy term, or that the terms match in yet another taxonomy assigned to AUTOREN? If the later, what taxonomy would that be?

    You say PUBLIKATIONEN posts may have multiple authors. WP normally associates only a single author with each post, how are additional authors kept track of? In other words, where in the database would one go to determine all of the authors? Are the authors managed as the AUTOREN taxonomy terms?

    You mention a link/button that goes with the PUBLIKATIONEN post. Will this appear on the single post page or on index and archive pages, or both?

    When it comes to placing buttons with posts, how to do so could depend on the theme. What theme are you using?

    I think I vaguely understand what you’re doing now, but I want my understanding to be even more clear before I suggest a solution. Thank you for your patience.

    Thread Starter gfaw

    (@gfaw)

    Hi bcworkz,

    first of all thank you for taking an interest in my problem and trying to help – greatly appreciate this!

    The CPT PUBLIKATIONEN has a custom Taxomony called AUTOREN which has Terms the names of the writers. They would resemble tags in the default WP taxonomy sceme.

    CPT AUTOREN only has terms, the names of the writers which resemble a post for each name.

    The slug is the same in both: http://www.example.com/autoren/<writer-name&gt; (as long as I use the same slug in both when I create the entry, of course!)

    Unfortunately I have it not online yet, but perhaps some screenshots will make is clearer?

    Screenshots

    On the PUBLIKATIONEN listing page you see the button “Mehr ΓΌber den Autor” – clicking this should bring up a list of writers profiles associated to the post. I’ve scrolled down a bit for the screenshot so to make clear that individual PUBLIKATIONEN posts have only one others have 3 or even more contributors

    Thread Starter gfaw

    (@gfaw)

    I found some code which might go in the right direction:

    $terms = get_the_terms_list( 'autoren' );
    
    echo '<ul>';
    
    foreach ( $terms as $term ) {
    
        // The $term is an object, so we don't need to specify the $taxonomy.
        $term_link = get_term_link( $term );
    
        // If there was an error, continue to the next term.
        if ( is_wp_error( $term_link ) ) {
            continue;
        }
    
        // We successfully got a link. Print it out.
        echo '<li> </li>'; // use single-autoren page template
    }
    
    echo '</ul>';

    Output:
    something that can be used within a link such as:

    <a href="<?php get_listing_link_for_terms( $post->ID, 'autoren'); ?>">Link to associated writers listing page</a>

    Thread Starter gfaw

    (@gfaw)

    As for buttons and the theme:

    I’m building on the roots theme and twitter bootstrap, though this is not theme related, I believe.

    The code of a button would look like this:
    <a class="btn btn-default hidden-lg" href="<?php echo get_post_type_archive_link( 'autoren' ); ?>" role="button">Mehr ΓΌber den Autor</a>

    The same button is used on the PUBLIKATIONEN single and archive pages.

    Moderator bcworkz

    (@bcworkz)

    Thank you for the detailed information! I now understand your setup. There still remains one point of confusion, which may simply be a language difference. We should come to an agreement on the proper terminology. I propose the English interpretation mainly because WP function names are in English. You say the CPT AUTOREN only has terms. At least in English WP documentation, only taxonomies have “terms”. CPTs do not have terms, they have names or slugs. While some array keys use “name”, I prefer to use “slug”. You seem to use “slug” and “term” interchangeably, lets stay with “slug” please πŸ™‚

    WP normally relates various components by ID. The reason you’re running into difficulty is you are relating publikationen authors to their respective CPT by relating autoren taxonomy terms to autoren CPT slugs, which is fine, except there are no predefined functions to get related autoren because of this.

    There’s a few ways to accomplish this, I suggest we use a custom page template because the code involved is more straight forward and easier to understand. If you haven’t already done so, you should create a child theme to contain your custom work. This protects it from any theme updates that would otherwise overwrite your custom code.

    The custom page template will pretty much be the same as the AUTOREN archive page, except the loop structure will be replaced by custom code. The inner code that outputs the bios, etc. remains, the while have posts; the post loop structure is what will change. You will add a single new page based on this template. All you need to do in adding this page is define a title so that a slug exists with which you can access the page. No content or anything else is necessary.

    More on the custom template later, let’s make the proper button link first. We will not be using the get_the_terms_list() code you found because most of the work will be on the custom template where we will need terms, but not links. The link button code will be fairly simple, it needs to be placed inside any PUBLIKATIONEN loop on all related templates. The link button can mostly be as in your last post, except the href attribute can be the static permalink to the page you create based on the custom template. All PUBLIKATIONEN post button links go to the same page.

    What will differentiate each link will be the post ID passed as an URL parameter. This is the only data the custom template needs to do its thing. If the custom template page’s ID turns out to be 123, the href code might look like this:
    href="<?php echo get_the_permalink( 123 ),'?pub=', get_the_ID() ?>"

    If the page slug were “autoren-bios”, we would get something like this href:
    http://example.com/autoren-bios?pub=123

    When the user clicks that link, the custom template is loaded. It gets the publikationen ID from $_GET['pub'] which allows it to get an array of AUTOREN taxonomy terms. This is used to drive the output loop, using foreach instead of while. In each iteration, extract the term name and use it to query for a AUTOREN CPT by slug/name (even though it is a slug, the query argument is “name”. (small wonder we’re confused over terminology πŸ™‚ )). You can query either by using get_posts() or by a custom WP_Query, the first is a wrapper for the second.

    When the query returns results, you run another loop (inside the foreach loop), this time it’s the while have posts; the post type of loop (but using a custom query object, not the global query), even though the query will have only returned a single post object. Inside this inner loop is where the usual output code goes.

    You might notice this is not very efficient as we need to run a separate query for each autoren. If your data isn’t to far developed, you might consider a different way of maintaining publikationen autoren besides by taxonomy term/CPT slug. An array or list of IDs of AUTOREN posts stored in PUBLIKATIONEN post meta would make for more efficient AUTOREN post queries because the entire ID array could be passed as a “post__in” argument, so all the autoren are returned in a single query.

    That does complicate creating the publikationen post to begin with, entering ID numbers is far from user friendly. Perhaps the user can input autoren slugs and a script runs on post save to create an ID array from the input slugs. Of course you then have to run multiple queries again to do this, but it’s once for every post save instead of every time any visitor clicks on the autoren link.

    Something to consider? It’s up to you. If your site is not really busy, it may not make any real difference.

    Thread Starter gfaw

    (@gfaw)

    Agreed on the terminology πŸ™‚ My native language is German and while my command of English is decent, it might give me issues with professional terminologies.

    As for the theme: roots is a Starter theme to build upon, no need to create a child theme.

    So far I have successfully created a page and linked to it – Works great!

    Now the query gives me some trouble…

    This is the code of the template, basically it’s the code of a default page template more or less.

    <?php
    /*
    Template Name: Autoren nach Publikation Template
    */
    ?>
    
    <?php get_template_part('templates/breadcrumbs'); ?>
    
    <?php if (!have_posts()) : ?>
      <div class="alert alert-warning">
        <?php _e('Sorry, no results were found.', 'roots'); ?>
      </div>
      <?php get_search_form(); ?>
    <?php endif; ?>
    
    <?php while (have_posts()) : the_post(); ?>
      <?php get_template_part('templates/content-publikationen-neuerscheinungen'); ?>
    <?php endwhile; ?>

    So that’s what I came up with, but it’s not working.

    <?php
    /*
    Template Name: Autoren nach Publikation Template
    */
    ?>
    
    <?php get_template_part('templates/breadcrumbs'); ?>
    
    <?php if (!have_posts()) : ?>
      <div class="alert alert-warning">
        <?php _e('Sorry, no results were found.', 'roots'); ?>
      </div>
      <?php get_search_form(); ?>
    <?php endif; ?>
    
    <?php 
    
    	$terms = get_terms( array( 'autoren' ) );
    
    	foreach ( $terms as $term ){
    
        $term = get_posts( array( 'post_type' => 'autoren' ));
    
        // If there was an error, continue to the next term.
        if ( is_wp_error( $term_link ) ) {
            continue;
        }
    
    while (have_posts()) : the_post();
     get_template_part('templates/content-single-autoren');
    endwhile;
    }
    ?>

    What am I doing wrong?

    Moderator bcworkz

    (@bcworkz)

    Your command of English is much more than decent my friend πŸ™‚ Where I was confused amounts to WP jargon that the vast majority of native English speakers would not know, so you can be excused for not getting it quite right.

    Yesterday I hadn’t yet taken a look at your theme. I’ve now taken a cursory look, I see how it allows for customization and a child theme actually may not even work. I will leave interfacing with your theme to you. My advice to copy a template as a starting point may not be the best as I had something else in mind at the time.

    The breadcrumbs part is possibly OK but the no posts/search form needs to go. It works off of the global query which is invalid for this template. It’s not needed because no one will load this page unless they clicked an author button for a publikationen post. You may find the breadcrumbs template may not work correctly, but hopefully it doesn’t break anything. We can repair that part later if need be. You should probably comment it out for now just in case.

    Your first task after breadcrumbs is to get the autoren terms assigned to the post whose ID was passed in the URL. You’ve used get_terms(), which will return all terms. You want only the terms assigned to the post ID passed, so use wp_get_post_terms(). You then run an outer foreach loop driven by the results.

    Inside the foreach loop you need to run a custom WP_Query to get the current author term’s autoren post. The resulting custom query loop is different than the global query loop you are using. All the template tags like have_posts() and the_post() are replaced by corresponding object methods such as $query->have_posts().

    Consider the following example as pseudocode to illustrate the basic structure. It should be valid PHP, but there might be incorrect syntax. There could be be some details wrong or omitted that you’ll need to fix or fill in. In particular the $args array is likely incomplete, but it should work as is for now.

    $post_authors = wp_get_post_terms( $_GET['pub'], 'autoren');
    foreach ( $post_authors as $author ) {
       $args = array( 'name' => $author->slug,
          'post_type' => 'autoren',
       );
       $query = new WP_Query( $args );
       while ( $query->have_posts() ): $query->the_post();
          //output autoren title, content, etc.
       endwhile;
    }

    There will still need to be error handling and perhaps other elements added, but this should at least get the basic data output.

    Thread Starter gfaw

    (@gfaw)

    Thanks for praise – learning a little every day πŸ˜‰

    So now it does something. It outputs Autoren nach Publikation (“writers by publication”) which is the title of this page to list the posts. And it does it in a way, as to if there is one writer one line is output and if there are 3 it outputs three lines.

    As for the rest:
    I removed the no posts/search bit.

    Breadcrumbs: I’m using the Breadcrumbs Nav XT plugin, this is just a bit of default code. I tried with temporarily removing the statement – made no difference. So I guess, there’s no issue here.

    Thread Starter gfaw

    (@gfaw)

    OK, I was a bit too quick trying to pull the template in the while-loop. When I use the_title(); it actually outputs the manes of the writers!

    Thread Starter gfaw

    (@gfaw)

    It works!!! That is so awesome! Thanks a million!

    Moderator bcworkz

    (@bcworkz)

    That’s great news! You are most welcome, glad to help.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Fetch posts list from first CPT taxonomy term and list under 2. CPT’ is closed to new replies.