Viewing 5 reply threads
  • Author
    Posts
    • #2209
      ianmuscat
      Member

      Hi,

      I have created a ‘Tests’ CPT using Piklist. I need a way to include the contents of a specific ‘Test’ on a specific page. To do this I have created a metabox that shows up on all pages – the idea is to allow the user to be able to choose from a select box which ‘Test’ they would like to show on that page.

      I am assuming that the user would be able to choose the ‘Test’ by name, however, when I then use get_post_meta() in the template part I shall be including, I would somehow need to pass a post id.

      Any idea on how this can be accomplished using Piklist? I read a bit about post-to-post relationships, but I’m not sure if it’s what I need.

      Additionally, I found an interesting thread by @Jason (https://piklist.com/support/topic/post-relate-select-dropdown/) on what I think is something similar, however, I’m not sure how this would work in my scenario.

      Any input/ideas would be most appreciated.

      Ian

    • #2226
      Steve
      Keymaster

      You can use something similar to this code to display the Post Titles in a dropdown:

      ,'choices' => piklist(
                    get_posts(
                       array(
                        'post_type' => 'post'
                        ,'orderby' => 'post_date'
                       )
                       ,'objects'
                     )
                     ,array(
                       'ID'
                       ,'post_title'
                     )
                  )
      

      This will save the Post ID, which you can then use to get the post.

    • #2228
      ianmuscat
      Member

      Hi Steve,

      This works like a charm. I was over complicating my solution. I can’t thank you enough!

      You may mark this as resolved.

      Ian

    • #2229
      Steve
      Keymaster

      Awesome! BTW, This code was pulled from the Piklist Demos > Advanced Tab > Content Section.

      The built-in demos has lots of cool example code.

    • #2231
      ianmuscat
      Member

      Awesome. I’ll take a closer look at the demos.

      Just out of curiosity, could you provide a scenario for using post-to-post relationships? – I’m not 100% sure I understood their purpose. Would this be similar to having two tables in a DB rated to one another?

      I did manage to get most of @jason ‘s code working for my requirements, but I am curious if I could use relate two posts and create select field as opposed to a checkbox section.

      Once again, thanks so much for your help!

      Regards,
      Ian

    • #2238
      Jason
      Keymaster

      Hi Ian!

      I made a select field a while back for post-to-post relationships: https://gist.github.com/JasonTheAdams/342ad36e9e98269e81c8

      The purpose of post-to-post relationships is to create the ability to reference a page/post in either direction. Let’s say I have a product post type and I decide I decide I want an faq post type as well (for an faq archive). I can then add a post-relate field to the faq post type for the products. At that point, when creating an faq post, I can select the corresponding product(s).

      From that point on, I can retrieve the corresponding faq post from the product, or I can select all the product(s) from the faq. So if I wanted to show the first 3 questions/answers on the product page, no problem.

      Hope this helps! 🙂

Viewing 5 reply threads
  • The topic ‘Listing CPT posts in a metabox using Piklist’ is closed to new replies.