• Resolved Jeff Kamo

    (@jeffkamo)


    Hello, I believe I have a similar question as that found in this thread. I wish to provide search functionality on my WordPress site where I have created associations between posts with the Posts 2 Posts plugin.

    Specifically, I have two custom post types: Products and Projects. I wish to be able to search my projects by the products. Is this possible?

    For example:

    I have several projects (such as “Hospital”, “Garage”, “House”, etc.) that have some of the following products associated to them: “Concrete-A”, “Concrete-B”, “Concrete-C”, etc. I now wish to use a search form to look for all projects that were created with “Concrete-B” and/or “C”. The search results would obviously show just the relevant projects, but not the products (which can be found in other parts of the site).

    Thank you in advance!

    https://www.ads-software.com/extend/plugins/posts-to-posts/

Viewing 4 replies - 1 through 4 (of 4 total)
  • I’m not 100% sure but maybe I can point you in the right direction…

    To show all projects made with product A, you would do:
    $connected = new WP_Query( array(‘post_type’ => ‘project’,’posts_per_page’ => -1,’orderby’ => ‘title’, ‘order’ => ‘ASC’,’connected’ => [PRODUCT ID]) );

    It makes sense to me that making any search script, then adding ‘connected’ => [PRODUCT ID] to the wp query would do exactly what you’re looking to do.

    Thread Starter Jeff Kamo

    (@jeffkamo)

    Thank you for you advice, I will give that a try and report back here when I’m done ??

    Thread Starter Jeff Kamo

    (@jeffkamo)

    Hey thanks a lot, your advice worked out. The only change I had to make was to add ‘connected_type’ to the query, and that made it work perfectly.

    My final code is as follows:

    <?php if ( $_GET['product-id'] ) {
      query_posts( array( 'post_type' => 'projects', 'connected_type' => 'products_to_projects', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'connected' => $_GET['product-id'] ) );
    } ?>
    Thread Starter Jeff Kamo

    (@jeffkamo)

    Okay a new problem with the above code. With the query_post done in this way, it overrides any attempt to search with additional features. So for example…

    https://www.mysite.com/?s=concrete&year=1999&product=5&post_type=projects

    This should return a series of all Project posts that contain “concrete”, has the taxonomy year of 1999, and is connected to product with ID 5.

    Unfortunately, with the query as it is now, it only returns all projects that are connected with the product ID 5, and ignores all other filters.

    Any idea how to fix it so the query includes other filters?

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: Posts 2 Posts] Search form to find posts by P2P association?’ is closed to new replies.