Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to construct a catalog page, made out of products and product-categorys. I have set up both post types, and set up a connection between them in functions.php (Using the Posts2Posts plugin.

The problem I'm having, is that in my catalog-page.php, I can't see the connected pages (I can see the Categories using the first loop, but not the connected Products).

Code:

functions.php (Relevant)

/* ======================
   Custom Page Types
   ====================== */

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'product-category',
                        array(
                             'labels' => array(
                                 'name' => __( 'קטגוריות' ),
                                 'singular_name' => __( 'קטגוריה' ),
                                 'add_new' => __('קטגוריה חדשה'),
                             ),
                             'public' => true,
                             'has_archive' => true,
                             'hierarchical' => true,
                             'supports' => array(
                                 'title',
                                 'editor',
                                 'thumbnail',

                                 'page-attributes'
                             )

                        )
    );
    register_post_type( 'product',
                        array(
                             'labels' => array(
                                 'name' => __( 'מוצרים' ),
                                 'singular_name' => __( 'מוצר' ),
                                 'add_new_item' => __('מוצר חדש'),
                                 'new_item' => __('מוצר חדש'),
                                 'parent' => __('קטגוריה'),
                             ),
                             'public' => true,
                             'has_archive' => true,
                             'hierarchical' => true,
                             'supports' => array(
                                 'title',
                                 'editor',
                                 'thumbnail',

                                 'page-attributes'
                             )
                        )

    );

}

/*
 * Posts2Posts plugin
 */
function my_connection_types() {
    p2p_register_connection_type( array(
                                       'name' => 'products_to_categories',
                                       'from' => 'product',
                                       'to' => 'product-category'
                                  ) );
}
add_action( 'p2p_init', 'my_connection_types' );

catalog-page.php (Relevant)

<?php if (have_posts()) {
while (have_posts()) : the_post(); ?>
    <article>
        <?php
        $category_arguments = array(
            "post_type" => "product-category",
        );
        $category_query     = new WP_Query($category_arguments);
        while ($category_query->have_posts()) {
            $category_query->next_post();
            echo "<h2>{$category_query->post->post_title}</h2>";
            $products_arguments = array(
                "connected_type"      => "products_to_categories",
                "connected_items"     => $category_query->post->ID,
                "connected_direction" => "from",
                "nopaging"            => true,
            );
            $products_query     = new WP_Query($products_arguments);
            var_dump($products_query->have_posts(), $category_query->post->ID);
            while ($products_query->have_posts()) {
                $products_query->next_post();
                echo "<h3>{$products_query->post->post_title}</h3>";
                var_dump($products_query->post);
            }

            var_dump($category_query->post);
        }

This outputs:

משקפי שמש

boolean false //have_posts() on products
int 81        //Post ID (correct)
object(WP_Post)[1992] //The category post
  public 'ID' => int 81
  public 'post_author' => string '1' (length=1)
  public 'post_date' => string '2012-12-16 23:47:02' (length=19)
  public 'post_date_gmt' => string '2012-12-16 21:47:02' (length=19)
  public 'post_content' => string 'קטגוריה של משקפי שמש' (length=37)
  public 'post_title' => string 'משקפי שמש' (length=17)
  public 'post_excerpt' => string '' (length=0)
  public 'post_status' => string 'publish' (length=7)
  public 'comment_status' => string 'closed' (length=6)
  public 'ping_status' => string 'closed' (length=6)
  public 'post_password' => string '' (length=0)
  public 'post_name' => string '%d7%9e%d7%a9%d7%a7%d7%a4%d7%99-%d7%a9%d7%9e%d7%a9' (length=49)
  public 'to_ping' => string '' (length=0)
  public 'pinged' => string '' (length=0)
  public 'post_modified' => string '2012-12-16 23:47:02' (length=19)
  public 'post_modified_gmt' => string '2012-12-16 21:47:02' (length=19)
  public 'post_content_filtered' => string '' (length=0)
  public 'post_parent' => int 0
  public 'guid' => string 'http://localhost/geva/?post_type=product-category&#038;p=81' (length=59)
  public 'menu_order' => int 0
  public 'post_type' => string 'product-category' (length=16)
  public 'post_mime_type' => string '' (length=0)
  public 'comment_count' => string '0' (length=1)
  public 'filter' => string 'raw' (length=3)
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.