WordPress.org

Support

Support » Plugins and Hacks » Custom Content Type Manager » search post on custom fied

search post on custom fied

  • Hello,

    I am new with both word press and cctm which is great and save me a lot of work. Thanks a lot for building this plug in !

    I have a custom type called Item which has a price.
    Now I wanted to search Post depending on some cutom field. For instance on the “Price” field and I would like to search for every post which price is higher or lower than a posted value.

    if I do that :
    $Q = new GetPostsQuery();
    $args = array();
    $args[‘post_type’] = ‘item’;
    $args[‘orderby’] = ‘post_title’;
    $args[‘price’] = ’10’;
    $results = $Q->get_posts($args)

    it will return items where price equals 10.

    Could you point me out a link or a quick sample code, how to achieve that. If I could avoid writting sql it would be good, if it is not possible I ll deal with it.

    thanks !

    http://wordpress.org/extend/plugins/custom-content-type-manager/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Ok after googling with some better keyword I found an answer :

    $args = array(
    	  'post_type'=> 'item'
    );
    
    $args['meta_query']= array(
    	array(
    		'key' => 'price',
    		'value' => 10,
    		'type' => 'numeric',
    		'compare' => '<'
    	)
    );
    
    $results = query_posts($args);
    print_r($results);
    Plugin Contributor fireproofsocks

    @fireproofsocks

    You can use query_posts if you want, but I found it to be maddeningly poorly conceived due to reliance on global variables and WP’s incessant habit of having unseen hooks in everything. That and WP_Query were the biggest motivators in me writing my own database API which became the GetPostsQuery class.

    To use GetPostsQuery to the same effect, you could modify your original query like this:

    $Q = new GetPostsQuery();
    $args = array();
    $args['post_type'] = 'item';
    $args['orderby'] = 'post_title';
    $args['price']['>'] = '10';
    $results = $Q->get_posts($args)

    Take a look at the wiki, specifically at the operators that are supported by GetPostsQuery: http://code.google.com/p/wordpress-custom-content-type-manager/wiki/get_posts#Operators

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘search post on custom fied’ is closed to new replies.
Skip to toolbar