0

I have two custom post types: Books and Subjects. For this projects, I'm using Super-CPT to create custom post types:

$books = new Super_Custom_Post_Type( 'book', '', 'book', $register = array(
        'supports' => array( 'title', 'editor', 'thumbnail'),
        )
);

$subjects= new Super_Custom_Post_Type( 'subject', '', '', $register = array(
        'supports' => array( 'title', 'editor'),
        'hierarchical' => true,
        ) 
);

To select which are the subjects of the book, I create a meta box and the values from Subjects:

$profissionais->add_meta_box( array(
        'id' => 'subjects',
        'context' => 'side',
        'fields' => array(
              'subjects' => array( 'type' => 'select', 'label' => '', 'data' => 'subject', 'multiple' => 'multiple' )
        )
) );

Now, I need to show on the page of each subject, the books with this subject. I start with this:

$args = array(
  'post_type' => 'book',
);
$books = new WP_Query($args);

But I do not know how to move forward. I'm trying to do something like this:

SELECT book_title FROM books WHERE subject = subject page title

UPDATE

Solved!

$current = get_the_ID();

 $args = array(
   'post_type' => 'book',
   'posts_per_page' => -1,
   'meta_key' => 'subjects',
   'meta_value' => $current,
 );

 $books = new WP_Query($args);
  • How does "Super-CPT" store those fields? Post meta? – s_ha_dum Nov 5 '13 at 15:57
  • @s_ha_dum The fields are store in wp_postmeta table: meta_key: subjects; meta_value: 78. This is the subject id. – marcelo2605 Nov 5 '13 at 16:13
1

If I understood correctly, you need something like the following:

$args = array(
    'post_type' => 'book',
    'posts_per_page' => -1,
    'meta_key' => 'subject',
    'meta_value' => YOUR_SUBJECT_PAGE_TITLE,
);
$books = new WP_Query($args);

Is this what you're trying to achieve?

Also, please have a look at WP_Query.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.