0

I've created an admin only page simply to view more information about subscribers to my website. The subscriptions are stored as Custom Post Types.

All I want to do is make a condition that looks up a list of cancelled subscribers and orders them by the expiry of the subscription. The subscription expiry is stored as yyyymmdd as a meta tag on the post.

There are a few pages about this, but I cannot get any solution to work in my instance and it's probably because I'm doing something stupid.

The two I have tried are:

     $args=array(
            'post_type' => 'subscription',
            'post_status' => 'publish',
            'meta_key' => 'expiry',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1,
            'meta_query' => array(
                                  array(
                                        'key' => 'paypal_recur_profile_status',
                                        'value' => get_post_meta(get_the_ID(),'cancelled',true),
                                        'compare' => '='
                                        )

                                  ),
            );

and

 $args=array(
        'post_type' => 'subscription',
        'post_status' => 'publish',
        'meta_key' => array('paypal_recur_profile_status', 'expiry'),
        'meta_value' => 'Cancelled',
        'orderby' => 'expiry'
        'order' => 'DESC',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1,
        );

The first returns nothing, the second seems to do ok but has some anomalies in it so assume it ends up ordering it by post number and the other correct order results are coincidental.

Can anybody spot the un-deliberate mistake in here or is this simply the wrong way to achieve what I'm trying to do?

  • What are the possible values of paypal_recur_profile_status? Is paypal_recur_profile_status the custom field indicating whether a subscription has been cancelled? – engelen Jun 27 '14 at 18:52
  • Yes @engelen the meta_key is paypal_recur_profile_status and it should be Cancelled in this case to start the query – Bysander Jun 30 '14 at 9:36
1

The value for the value key in the meta query is probably the cause of the problem.

 'value' => get_post_meta( get_the_ID(), 'cancelled', true )

The get_post_meta call actually retrieves the value of the meta key "cancelled" for the current post. So, in your current WP_Query, you're retrieving all posts with a paypal_recur_profile_status equal to the value of the custom field "cancelled" of the current post.

If you want to get posts that have a profile status of Cancelled, you should check value to be "Cancelled":

$args = array(
    'post_type' => 'subscription',
    'post_status' => 'publish',
    'meta_key' => 'expiry',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'posts_per_page' => -1,
    'ignore_sticky_posts'=> 1,
    'meta_query' => array(
        array(
            'key' => 'paypal_recur_profile_status',
            'value' => 'Cancelled'
        )
    )
);

Furthermore, caller_get_posts was replaced by ignore_sticky_posts in WordPress 3.1 (so I've replaced that), and you can omit the compare key in the meta_query, as that's = by default.

  • Yeah, absolutely perfect @engelen thank you. Have accepted and +1 for helping me out - it's the problem with teaching yourself PHP - sometimes the basic mistakes get missed. – Bysander Jun 30 '14 at 14:36

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.