0

Under 4.5, I have posts with meta_key as series and meta_value as 19, 20, etc

I am trying to loop through them in a template, and I guess I'm a special kind of dense.

Most recently I have tried

$args = [
    'post_type' => 'series',
    'posts_per_page' => -1,
    'meta_key' => 'series',
    'meta_query' => [
        'key' => 'series',
        'value' => 19,
        'compare' => '=',
    ],
];

$posts = new WP_Query( $args );

It seems to return empty objects, or a result of one, depending on what I change.

Is there some trick to looping through, maybe?

  • Are these values serialized? As in, did you save an array of values? I'm not sure if it matters but are they strings or integers? '19' and 19 may be different in MySQL. – Howdy_McGee Apr 15 '16 at 21:51
3

I am assuming your custom post type is also "series". Can you try this, it should work.

 $args = [
        'post_type' => 'series',
        'posts_per_page' => -1,
        'meta_query' => [
            [
            'key' => 'series',
            'value' => 19,
            'compare' => '=',
            'type'=>'NUMERIC'
            ]
        ],
    ];

    $posts = new WP_Query( $args );

or you can simple use:

$args = [
            'post_type' => 'series',
            'posts_per_page' => -1,
            'meta_key'=>'series',
            'meta_value'=>19,
            'meta_compare'=>'=',
            'meta_type'=>'NUMERIC'
        ];

        $posts = new WP_Query( $args );

If you are using these meta values as NUMERIC it will be essential to provide "meta_type", as WordPress treats "Numeric" and "Char" values very differently. As you seems to use these value as Number, you need to provide "meta_type"

  • I think meta_query takes an array of arrays: meta_query => array( array() ). Could you also explain what you did and why? – Howdy_McGee Apr 15 '16 at 22:00
  • @Howdy_McGee, yes! let me edit it. – Deepak jha Apr 15 '16 at 22:03

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.