6
6
666Wolf6662016-05-05 11:47:43
WordPress
666Wolf666, 2016-05-05 11:47:43

get_post() doesn't work with custom field, what's the problem?

The essence of the problem is as follows.
It is necessary to display posts sorted by an arbitrary field, but nothing works ... look at the code, maybe you will see something, thanks, I've been struggling with this for two days now ...

if ($rand == 1 ){
      $args = array(
    'post_type' => 'wsp_site', 
    'portfolio' => $port, 
    'numberposts' => $count,
    'tax_query' => array(
              array( 
                'taxonomy' => 'portfolio',
                'field'    => 'term_id',
                'terms'    => $id_port,),),
    'orderby' => 'rand',
        );
    wsp_view ($sites = get_posts ($args), $col);
    }
    if ($rand == 2 ) {
      $args = array(
    'post_type' => 'wsp_site', 
    'portfolio' => $port, 
    'numberposts' => $count,
    'tax_query' => array(
              array( 
                'taxonomy' => 'portfolio',
                'field'    => 'term_id',
                'terms'    => $id_port,),),
    'orderby' => 'wsp_weight', /*здесь проблема*/ 
    'order' => 'ASC'
        );
    print_r($args);
    wsp_view ($sites = get_posts ($args), $col);
    }

The name of the custom field is exactly right, the random display part works. In what direction to move?
I decided so far by iterating over the array using the Bubble method, but this is harsh ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nikolaev, 2016-05-05
@666Wolf666

Custom fields are meta data that is stored in a special way. Therefore, you need to order the selection by meta_value, and pass the field name in meta_query (or meta_key). This is how it should work.

$args = array(
    'post_type' => 'wsp_site', 
    'portfolio' => $port, 
    'numberposts' => $count,
    'tax_query' => array(
              array( 
                'taxonomy' => 'portfolio',
                'field'    => 'term_id',
                'terms'    => $id_port,),),
    'orderby' => 'meta_value',
    'meta_key' => 'wsp_weight',
    'order' => 'ASC'
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question