• Resolved GideonKimani

    (@gideonkimani)


    I’d like to connect the locations post type with the product post type created by woocommerce using posts-to-posts.
    I’ve been able to register a connection type using:
    ‘ /** Connecting Posts to Posts and Pages such as Locations to Shop Products. */
    function my_connection_types() {
    p2p_register_connection_type( array(
    ‘name’ => ‘locations_to_product’,
    ‘from’ => ‘location’,
    ‘to’ => ‘product’
    ) );
    }
    add_action( ‘p2p_init’, ‘my_connection_types’ ); ‘

    I then added this code to single-location.php:
    ‘ /* Connect a location to products */
    $connected = new WP_Query( array(
    ‘connected_type’ => ‘locations_to_product’,
    ‘connected_items’ => get_queried_object()
    ) );

    echo ‘<p>Related products:</p>’;
    echo ‘

      ‘;
      while( $connected->have_posts() ) : $connected->the_post();
      echo ‘

    • ‘;
      the_title();
      echo ‘
    • ‘;
      endwhile;
      echo ‘


    ;

    wp_reset_postdata(); ‘

    The meta boxes are in place but for some reason they don’t work.Is there a problem with the code or am I adding it to the wrong page?

    https://www.ads-software.com/extend/plugins/events-manager/

Viewing 6 replies - 1 through 6 (of 6 total)
  • sorry, I haven’t tried this issue before and I’m afraid I can’t give you an advice about this. However, have you tried to re-copy your single.php and then rename it to single-location.php, is this what you did?

    Thread Starter GideonKimani

    (@gideonkimani)

    Yes this is what I did. The idea is to link events and locations to their products in the online shop and display those products on the event or location page.

    Plugin Author Marcus (aka @msykes)

    (@netweblogic)

    he meta boxes are in place but for some reason they don’t work

    Interesting idea, although I can’t give much advice given I’ve not used Posts2Posts myself (although I’ve heard it works well with EM). What doesn’t work specifically?

    It could also be WC blocking the use of the product CPTs in queries, have you tried other non WC cpts?

    Thread Starter GideonKimani

    (@gideonkimani)

    Hi Marcus,
    Using Posts 2 Posts, nothing works, even the meta boxes in the admin end don’t show up and it breaks the accordion layout I use for the single location and single events pages.
    This is the code I used Maybe I’ve overlooked something?

    Thread Starter GideonKimani

    (@gideonkimani)

    I added the location-single.php code to the single-product.php page as well so the meta boxes are back in the back-end but the link is dead, it just takes me to the top of the page.

    Plugin Author Marcus (aka @msykes)

    (@netweblogic)

    Icouldn’t help you with specifics unfortunately, but one thing I can say is that copying the location-single.php file contents to your theme probably won’t work, because you don’t have the relevant variables defined.

    e.g. you probably need to define $EM_Location first:

    global $post;
    $EM_Location = em_get_location($post);
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Connecting with other CPTs’ is closed to new replies.