Support » Plugin: Pods - Custom Content Types and Fields » Create a URL that links to the latest post in a category

  • Resolved Sarah H

    (@sarahrbcom)


    I have a CPT Pod “Document” with a category “Agendas” and I need to link to the single post page of the most recently posted Agenda on the front page of the website (icon in header on right).

    Can this be done with wp_query or find()? I am not that PHP savvy at all but I know how to add stuff to functions.php. I’ve seen lots of code snippets doing this for blog posts but only one relating to Pods which doesn’t tell me much:
    https://wordpress.stackexchange.com/questions/254557/linking-to-the-most-recent-post-in-a-custom-post-type

    Even with the code snippets, I don’t know how to structure the URL parameters. I’ve experimented some but with no luck.

    Thanks in advance. Really enjoying PODs.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Paul Clark

    (@pdclark)

    Video walkthrough

    <?php
    
    add_shortcode(
    	'latest-agenda-document',
    	function(){
    		ob_start();
    
    		$posts = get_posts([
    			'post_type' => 'document',
    			'posts_per_page' => 1,
    			'orderby' => 'date',
    			'order' => 'DESC',
    			'tax_query' => [
    				[
    					'taxonomy' => 'document_category',
    					'field' => 'slug',
    					'terms' => 'agenda',
    				],
    			],
    		]);
    
    		printf(
    			'<p><a href="%s">%s</a></p>',
    			get_permalink( $posts[0]->ID ),
    			get_the_title( $posts[0]->ID )
    		);
    
    		return ob_get_clean();
    	}
    );
    Thread Starter Sarah H

    (@sarahrbcom)

    Thank you so much for the response Paul, the video walkthrough is very helpful!

    I’ve added this to the functions.php of my child theme and tested it by adding it to a few blog posts, but it seems to be returning the title and permalink of whatever post its in. For example:

    http://ncrcd.rb-com.com/uncategorized/hello-world/
    http://ncrcd.rb-com.com/uncategorized/another-sample-post/

    I’ve checked the code and it all matches, could there be something in my POD setup that’s creating this? Any thoughts? Much appreciated.

    –Sarah

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @sarahrbcom

    I cannot access the links you’ve posted.
    The code Paul posted should work fine as it specifically targets the document post type, not other post types.

    Cheers, Jory

    Thread Starter Sarah H

    (@sarahrbcom)

    Hi Jory,

    Thanks for the response. Can you be more specific as to why you can’t access the links? Do you see an error message? The site is discouraging search engines but is otherwise entirely public. I can open the links in an incognito window, so logins and cookies are not an issue. It currently has no SSL, is that the issue for you?

    I understand how the code targets the document CPT, which is why I’m confused as to the results I’m seeing.

    Can you view this screenshot? https://www.dropbox.com/s/zkdwx7mrofch3ie/shortcode-test-1.png?dl=0
    I added class="shortcode-test" to the <p> tag to confirm that the shortcode was indeed printing that line, but the string it’s returning does not seem to be the correct permalink and title.

    Thread Starter Sarah H

    (@sarahrbcom)

    Came back to this with fresh eyes and picked it apart, and discovered that the code above uses agenda as the term when it’s actually agendas. With that change it works now! I’ve styled it as a button with generic text and it’s working great now. I’m sure this will be useful in the future for other projects, thanks again.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.