• Resolved Cornelis Scheltinga

    (@cscheltinga)


    I’m working on a plugin which i’m going to make available for free.

    I’ve made a custom post type called ‘popup’, and I’ve created a custom field in which users can choose where the popup should appear. So they fill in page ID (comma seperated). I put those page’s in 1 array.

    Now comes the challenging thing:

    How do I query for a value in an array?

    I thought of something like this:

    $args = array(
    		'post_type' => 'notifications',
    		'posts_per_page' => 1,
    		'orderby' => 'menu_order',
    		'order' => 'ASC',
    		'meta_query' => array(array(
    			'key' => 'page_ids',
    			'value' => '103'
    		))
    	);

    The problem is; there is not just a ‘103’ in the page_ids key. There is an array which contains the page 103.

Viewing 1 replies (of 1 total)
  • Thread Starter Cornelis Scheltinga

    (@cscheltinga)

    Fixed it:

    $bar_key = null;
    	$page_id = 'none';
    
    if ( is_page() && !is_front_page() ){
    		$bar_key = 'cs_bar_pages';
    		$page_object = get_queried_object();
    		$page_id = get_queried_object_id();
    	}
    	else if ( is_front_page() ) {
    		$bar_key = 'cs_bar_homepage';
    		$page_object = get_queried_object();
    		$page_id = get_queried_object_id();
    	}
    
    $args = array(
    		'post_type' => 'notifications',
    		'posts_per_page' => 1,
    		'orderby' => 'menu_order',
    		'order' => 'ASC',
    		'meta_query' => array(
    			'relation' => 'OR',
    			array(
    			'key' => $bar_key,
    			'value' => 'yes',
    			),
    			array(
    			'key' => 'cs_bar_specific_pages',
    			'value' => $page_id,
    			'compare' => 'LIKE',
    			),
    			array(
    			'key' => 'cs_bar_specific_posts',
    			'value' => $post_id,
    			'compare' => 'LIKE',
    			),
    		)
    	);

Viewing 1 replies (of 1 total)
  • The topic ‘Custom field page id's’ is closed to new replies.