• Is it possible to use regex when providing meta_key?

    I have unknown number of meta_keys to check; location-1, location-2, location-3 and so on. They can be few or dozens for different users.

    I also tried using 'key' => array('location-1','location-2', 'location-3'), and it gives following error.

    Warning: trim() expects parameter 1 to be string, array given in \wp-includes\class-wp-meta-query.php on line 584

    $args = array(
        'meta_query' => array(
            'relation' => 'OR',
             array(
                    'key' => 'location-/(\d+)$/',
                    'compare' => '=',
                 ),
                 array(
                    'key' => 'location-/(\d+)$/',
                    'compare' => 'NOT EXISTS'
                 )
            ),
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
    );

    Looking forward to your tips. Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello Waqas,

    If want check for multiple meta_key and its value you have to write those all in meta query with meta_key as a string “location-1″ ,”location-2” etc. You are passing it an array which is not allowed by meta query.
    Please let me know if need any further help.

    Thanks

    Thread Starter muhammadwaqas

    (@muhammadwaqas)

    I understand that array isn’t acceptable for ‘key’ also I know hardcoding all key works.

    The problem is not only these keys are different for each user but also are different in numbers.

    For example; some users can have 3 locations and some can have 15 or so.

    Hello Waqas,

    So You need check first number of locations of user and then loop through these location and use the meta_query and add those location as meta_key.

    Thanks

    Thread Starter muhammadwaqas

    (@muhammadwaqas)

    Thanks for the reply. I think thats the only solution for this. Creating multiple meta_query using foreach;

    $locations = array('location-1','location-2', 'location-3');
    
    $main_array = array();
    foreach ($locations as $area) {
    
         //how do I do this part??
         $main_array[] = array(
            'relation' => 'OR',
             array(
                    'key' => $area,
                    'compare' => '=',
                 ),
                 array(
                    'key' => $area,
                    'compare' => 'NOT EXISTS'
                 )
    }
    
    $args = array(
        'meta_query' => array( $main_array ),
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
    );
    $query = new WP_Query($args);

    PS: Even if it was possible to use regex. That wouldn’t be a good idea either. Because then it will include all the locations ignoring which locations related to which user. ??

    if you need help in your code explain what you want to do. You have not explained your problem. You just want something which isn’t possible by default. There can be multiple ways of doing what you need. Explain your motive instead of what you think is the only solution. What are you trying to accomplish.

    If you are not investing time in asking the right question, expecting people to help you with right answers, seldom work.

    I think the problem in your case is how you store the data. You can store multiple datasets under one meta_key. Go ahead and read https://developer.www.ads-software.com/reference/functions/add_post_meta

    The parameter mixed $meta_value can take an array. The value of the custom field which should be added. If an array is given, it will be serialized into a string.

    Then you can use get_post_custom() function https://developer.www.ads-software.com/reference/functions/get_post_custom to get all the custom data against your post ID and store it in a variable. Var dump it and you will see a seriallized array of data all stored against the location meta_key.

    After which you can use maybe_unserialize() function and unserialize the array while you store it inside a variable. That varaiable will have all the values of your meta_key which you could easily loop through.

    $this->the_meta_data = get_post_custom( $the_rental_ID );

    return maybe_unserialize( $this->get_meta( $this->meta_keys[‘group_amenities’] ) );

    Here you go, I built an example class to get custom meta data from a meta_key that has serialized array stored inside it. https://gist.github.com/46ce2601518aff15610306b93d259db7

    Class code pasted below as well.

    <?php
    /**
     * WP_Get_Meta.
     *
     * Get class for the post_type.
     *
     * @since 1.0.0
     */
    
    if ( ! class_exists( 'WP_Get_Meta' ) ) :
    
    class WP_Get_Meta {
    
    	/**
    	 * The Location ID.
    	 *
    	 * @var 	int
    	 * @since 	1.0.0
    	 */
    	 public $the_location_ID;
    
    	/**
    	 * The Meta Data.
    	 *
    	 * @var 	array
    	 * @since 	1.0.0
    	 */
    	public $the_meta_data;
    
    	/**
    	 * Meta Keys.
    	 *
    	 * @var 	array
    	 * @since 	1.0.0
    	 */
    	private $meta_keys = array(
    		// All the meta keys.
    		'location' => 'location'
    	);
    
    	/**
    	 * Constructor.
    	 *
    	 * Checks the location ID and assigns
    	 * the meta data to $the_meta_data.
    	 *
    	 * @since 1.0.0
    	 */
    	public function __construct( $the_location_ID = NULL ) {
    		// Check if there is $the_location_ID.
    		if ( ! $the_location_ID ) {
    			$the_location_ID = get_the_ID();
    		} else {
    			$the_location_ID = intval( $the_location_ID );
    		}
    
    		// Assign values to the class variables.
    		if ( $the_location_ID > 0 ) {
    			$this->the_location_ID = $the_location_ID;
    			$this->the_meta_data = get_post_custom( $the_location_ID );
    		}
    	}
    
    	/**
    	 * Get Location: Meta.
    	 *
    	 * Gets the location meta_value if passed
    	 * a meta_key through argument.
    	 *
    	 * @since 1.0.0
    	 */
    	public function get_meta( $meta_key ) {
    		// Solves undefined index problem.
    		$the_meta = isset( $this->the_meta_data[ $meta_key ] ) ? $this->the_meta_data[ $meta_key ] : false;
    
    		// Array or not?
    		if ( is_array( $the_meta ) ) {
    			// Check 0th element of array
    			// If meta is set then return value else return false.
    			if ( isset( $the_meta[0] ) ) {
    				// Returns the value of meta.
    				return $the_meta[0];
    			} else {
    			    return false;
    			}
    		} else {
    			// If meta is set then return value else return false.
    			if ( isset( $the_meta ) ) {
    				// Returns the value of meta.
    				return $the_meta[0];
    			} else {
    			    return false;
    			}
    		}
    	} // get_meta() ended.
    
    	/**
    	 * Get Location: ID.
    	 *
    	 * @since 1.0.0
    	 */
    	public function get_ID() {
    		return $this->$the_location_ID;
    	}
    
    	/**
    	 * Get Location: Group Amenities.
    	 *
    	 * @since 1.0.0
    	 */
    	public function get_group_amenities() {
    		// Returns false if ID is not present.
    		if ( ! $this->the_location_ID ) {
    		    return false;
    		}
    		return maybe_unserialize( $this->get_meta( $this->meta_keys['group_amenities'] ) );
    	}
    
    } // class <code>WP_Get_Meta</code>  ended.
    
    endif;
    Thread Starter muhammadwaqas

    (@muhammadwaqas)

    Thanks Awais I’ll look into this. I know about serialized data in post_meta. Infact I’m saving some of my site data in this manner.

    But specifically for locations I wanted to keep it simple because that meta key is being used at several other functions.

    Thanks for your time. I’m on to building it now.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘regex meta key for meta_query’ is closed to new replies.