1

I am having issue with WP_Query, My scenario: a) Using http://wordpress.org/extend/plugins/countries/ b) Using WP_Query to fetch countries and their codes only. c) resetting query properly. d) using custom post type e) using meta boxes f) using get_post_type() to retrieve the custom post type.

Here is code snippet:

$query_args = array(
    'post_type' => 'countries',
    'posts_per_page' => -1,
    'post_status' => 'publish'
);
$countries_query = new WP_Query($query_args);
$countries_array = array();
if ($countries_query->have_posts()):
    while ($countries_query->have_posts()) :
        $countries_query->the_post();
        $country_meta = get_post_custom($post->ID);
        $country_code = $country_meta['country_code'][0];
        //$countries_array[$country_code] = $post->post_name;
        $countries_array[$country_code] = $post->post_title;
    endwhile;
endif;
wp_reset_postdata();
/*
 * sorting countries alphabatically, keeps keys intact with "ksort"
 */
ksort($countries_array);
$GLOBALS['custom_countries'] = $countries_array;

and I am adding meta box to my custom post type, using

if (get_post_type() == 'my_custom_post_type'): 
add_my_meta_box_for_custom_post_type();
endif;  

and get_post_type() returns "countries" instead of global $post which is in this case my "my_custom_post_type".

Is there anything wrong with my code?????

Please reply only if you have specific answers not general answers or speculations.

2
  • Where do you have this code? Are you in the Frontend or in the admin?
    – fischi
    Commented Jan 17, 2013 at 12:17
  • I am using it in backend and frontend both locations. but the get_post_type() check is only in backend. I have it inside if(is_admin())
    – Syed
    Commented Jan 17, 2013 at 14:27

2 Answers 2

1

If you look at the source and the Codex page for get_post_type you will see that returns the type of the "current post" unless you pass it a post ID or post Object. get_post_type should work without a parameter if you are running that code on the post edit page(s) generated by WordPress for post types. Anywhere else and that is not guaranteed. It isn't possible to give you specifics because your questions doesn't contain specific enough information, but I am fairly sure that is the problem you are having with the code.

That said, @WPThemes is right. That check is not necessary. That answer should probaly solve things for you (so pick it).

Additional notes: If you register your metaboxes on a callback (@WPThemes' answer) when you register the post type you can dispense with the is_admin check. The boxes will register when they are needed. you can also hook directly to add_meta_boxes_{post-type}.

1
  • Thanks for your reply, yes I am aware of that if I don't pass object or object ID to get_post_type it will retrieve the current global object beign process (in my case my custom post type while on its edit screen). I was just using the check for making my code looks more clear (for me to remember), otherwise I am already hooking meta box to my meta box properly. And I am using is_admin as well :). Thanks for your reply.
    – Syed
    Commented Jan 18, 2013 at 6:33
0

You don't need to check for custom post type to add meta boxes. You only need to specify which custom post type you'd like the add_meta_box() function you'd like to run it on. Something like this:

add_action('add_meta_boxes', 'add_my_meta_box' );
/**
 * Add meta box only to my custom post type
 */
function add_my_meta_box(){
    $my_custom_post_type = 'my_custom_post_type';
    add_meta_box( 'my-meta-box-id', 'Title of my Meta Box', 'my_callback_function', $my_custom_post_type, 'normal', 'high' );
}

/**
 * Shows the meta box options
 */
function my_callback_function(){
    /** Display your meta box options/fields **/
}
3
  • Thanks for your reponse, I am not having issue with hooking into my meta box, the only issue I was having is, when I am calling my WP_Query code on functions.php load, and pass it to global variable (so it is available for my add_meta_boxes hook so I can pass it to my custom field, and note that I am also using same WP_Query code in frontend as well), it was setting the global $post object (even though I am properly reseting the WP_Query).
    – Syed
    Commented Jan 18, 2013 at 6:48
  • So I have debugged and found that when I call WP_Query, at that time global $post object is empty so what WP_Query does is (seems like a bug), it attaches my current WP_Query first post to that empty global $post, as it sees its empty so it fix the emptiness ;), so when later my custom post edit screen loads it doesn't (re)set the global $post object, which is weired. Hope my explanation makes sense.
    – Syed
    Commented Jan 18, 2013 at 6:53
  • 1
    I solved the issue by utilizing get_posts instead of WP_Query, and for some strange reasons it works fine (without modifying the global $post or $wp_query). So do you have any explanation for this???? Only thing I am not doing is using setup_postdata().???? Any thoughts????
    – Syed
    Commented Jan 18, 2013 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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