0

Here are the arguments for my query.

The query always shows the same order after every refresh.

Explanation to the query: it show only 4 posts of the custom post type projekte. The post has a custom meta field called ecpt_skills and it should check if there is a string in it which is stored in the variable $porjektart. It also should not show the current post. And it should order the query by random. Everything works except the random.

 $args = array( 'posts_per_page' => 4,  
'post_type' => 'projekte', 
'meta_key'  => 'ecpt_skills', 
'meta_value' => $projektart, 
'meta_compare' => 'LIKE', 
post__not_in => array(get_the_ID()),
'orderby'=> 'rand' );

Does somebody know why this happens?

1

You could use meta_query to search in the meta data, and then sort them as random:

$args = array(
        'posts_per_page' => 4,  
        'post_type' => 'projekte', 
        'orderby'=> 'rand',
        'post__not_in' => array( get_the_ID() ),
        'meta_query' => array(
            array(
                'key' => 'ecpt_skills',
                'value' => $projektart,
                'compare' => 'LIKE',
            )
        ),
    );

There is also a mistype in your code, the post__not_in should be written as 'post__not_in'.

And also, if your meta value is not numeric, you should use meta_value instead of meta_value_num.

  • Thanks for your answer. But still it always shows the same order. – mhmma Jul 23 '17 at 9:36
  • @mhmma Although you have found the solution, but I've updated the answer for future reference. – Jack Johansson Jul 23 '17 at 9:58
1

Found the problem.

I use the custom post type order plugin.

So random order doesn't effect the query.

You have to insert 'ignore_custom_sort' => true, into your custom query to get it to work.

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.