WordPress.org

Ready to get started?Download WordPress

Forums

set multiple values for a single parameter in get_posts (8 posts)

  1. kanika
    Member
    Posted 2 years ago #

    Hi,

    I donot know where this question belongs so kindly excuse me if it's in the wrong forum!

    My problem:
    I am retrieving posts through the get_posts function as:

    <?php
    global $post;
    $args = array( 'numberposts' => 2, 'offset'=> 0, 'post_type' => 'webcast');
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post);
    $p_ID = get_the_ID();
    ?>

    I want to say now that the meta_value parameter should be equal to either XYZ or ABC

    How do I do that?

  2. esmi
    Forum Moderator
    Posted 2 years ago #

  3. kanika
    Member
    Posted 2 years ago #

    It could help if the parameter could negated as in

    meta_value <anything but> 'Live'

    Is it possible?

    Thanks,
    Kanika.

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with this [untested]:

    $args = array(
            'posts_per_page' => 2,
            'post_type' => 'webcast',
     	'meta_query' => array(
     		array(
     			'key' => 'yourmetakey',
     			'value' => array('XYZ', 'ABC'),
     		)
     	)
      );
     $myposts = get_posts( $args );
  5. kanika
    Member
    Posted 2 years ago #

    Thanks,

    That helped a lot! What worked for me was:

    //...code
                   'meta_query' => array(
                   'relation' => 'OR',
    		array(
    			//code
    		),
    		array(
    			//code
    		)
    )

    as given on the link u provided.

    I just have one more question. If I want to override the order_by according to the 'meta_value' parameter? As in, first we have all posts with meta_value 'XYZ' and then with 'ABC'

    Thanks,
    Kanika.

  6. kanika
    Member
    Posted 2 years ago #

    Ordering by Date has temporarily solved the problem. (Per chance) But i'd still like to get an answer to this one.

    Thanks,
    Kanika.

  7. keesiemeijer
    moderator
    Posted 2 years ago #

    As in, first we have all posts with meta_value 'XYZ' and then with 'ABC'

    Try it with two loops (get_posts), one for 'XYZ' and one for 'ABC'.

  8. kanika
    Member
    Posted 2 years ago #

    Oh, ok. Thought it could be clubbed into one.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags