shortcode – Show in an entry a Custom Post Type associated to a post


This is the information of my problem:

Technologies used: Divi Theme – WordPress

Show an Advanced Custom Field of type post_object related to a specific post.


  • I have a custom post type called countries which is a list of countries
  • I have an Advanced Custom Field called country of type post object that refers to that list of countries from the CPT
  • I have a Custom Post Type called News where I assign a country to each news using the Advanced Custom Field Country

What I wish:
I want to show in the entry of each news post the country associated with that entry. When I try to use Divi’s dynamic content I get the error: “Object of class WP_Post could not be converted to string”.

My code:
I have made this shortcode but it shows me a list of countries and doesn’t show the country assigned to the custom post type in the post entry. In the image, you can see what I want along with what I have at the moment.

See the Image

This is my code:

    add_shortcode( 'showpost', 'display_custom_post_type' );

    function display_custom_post_type(){
        $args = array(
            'post_type' => 'countries',

        $string = '';
        $query = new WP_Query( $args );
        if( $query->have_posts() ){
            $string .= '<ul>';
            while( $query->have_posts() ){
                $string .= '<li>' . get_the_title() . '</li>';
            $string .= '</ul>';
        return $string;

Any ideas?

Marlo Muñoz 1 year 2021-09-10T16:24:03-05:00 0 Answers 0 views 0

Leave an answer