I have a custom post type called "issue" and a contents page for each issue. I tag all of my articles with each issue by the issue number, so for instance, all articles with Issue 15 are tagged 'issue-15'.

I have a custom post template for contents and I use the following code to call up all issue posts within the same issue number.

$wp_query = new WP_Query( array( 
    'post_type' => 'issue',
    'tag' => 'issue-14',
    'posts_per_page' => 50
) );

Where it says 'issue-14', I need that to change based on a custom metabox field called 'issue-number' which only contains the number of the issue. So if the metabox field is "12" I need the tag to be "issue-12".


It sounds like you're just looking for get_post_meta(). Using your custom field of issue_number, you'll do something like this (untested):

$issue_number = get_post_meta( get_the_ID(), 'issue_number', true );
$my_issue_query = new WP_Query( array(
'post_type' => 'issue',
'tag' => 'issue-' . $issue_number,
'posts_per_page' => 50

Note: While the code you pasted may work, you don't want to override the $wp_query global with a new WP_Query instance. Rather, use a new prefixed query-specific variable to hold the query instead and then call it like $my_issue_query->the_post();

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.