0

This is the information of my problem:

Technologies used: Divi Theme - WordPress

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

Details:

  • 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',
            '$post->ID'
        );

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

Any ideas?

1
  • It sounds like what you're trying to do is associate one CPT (Country) with another CPT (News). So each News post can be associated with one or more Countries and vice-versa. The way I usually do this is via the ACF "Relationship" field, as explained here: advancedcustomfields.com/resources/relationship (I also use the "ACF Post 2 Post" plugin which automatically makes that relationship bi-directional.)
    – Michelle
    Sep 10 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.