Home Forums General Issues Get custom post type by value?


Get custom post type by value?

  • OK, I simply have to admit that I simply don’t get how to pull ANYTHING from the DB in WordPress … my head keep exploding and my brain resets every fifteen minutes (yeah, with a head like that it’s freakin’ hard to get anything done):

    How do I get a specific custom post type by value?

    I have created a Field Group called Brands, and under that I have a Field Name of ‘select_brands’. And I need to get one of a specific Brand.

    This obviously doesn’t work:

    $args_brand = array(
    	'post_type' => 'brands',
    	'numberposts' => 1,
    	'meta_query' => array(
    			'key' => 'select_brands',
    			'value' => '"' . $brand . '"',
    			'compare' => 'LIKE'

    This does neither:

    $args_brand = array(
    	'post_type' => 'brands',
    	'numberposts' => 1,
    	'meta_key'	=> 'select_brands',
    	'meta_value'	=> $brand

    Why is it that I simply cannot grasp this concept!?

    SELECT * FROM custom_post_type WHERE value = "brand"?

  • Can you upload or post the exported field group. If you’re using ACF5, export in json format and attach it as a .zip file. If you’re using ACF4 then the php code export will work.

  • Hi John, how do I do that?

  • ACF5 go to Custom Fields => Tools
    ACF4 Custom Fields => Export

  • Oh sorry, John, thought I was on a different post. I managed this last night after about five or six hours of work, randomly trying all combinations of field_value, term_id, slug … I could think of. Well, the lot. For HOURS. So frankly I don’t know what actually made it work, as it’s all very random to me. All I know is that I ended up with an extremely long script where I call the database multiple times. And to be honest, I don’t think it’s good for performance, at all.

    And now AGAIN (different project) I can’t get simple fields from a custom post type. It somehow turns into a bleedin’ nightmare every time I (try) use this plugin … and I seriously cannot grasp why.

    I just sent a ticket (again) for the exact same issue I start out with every time I use this plugins. It’s really sad, actually.

  • OK, found this out after banging my head at the wall: Am I right if I say that when setting up posts I have to use the exact characters ‘$post’?

    I was trying to: foreach ( $principles as $principle ) : setup_post( $principle ).

    Then: echo get_field(‘principle_link);

    And doing that I got nothing, it wasn’t until I (again, a reaction of desperation) changed it all to $posts and $post … !?

    Am I being struck by bad luck, or is this how WP is SUPPOSED to work!?

  • Sorry I didn’t get back to you sooner on this, it been a busy week.

    Yes, what you use wp_setup_postdata() you need to use $post to get values. $post is a global WP variable that is available in all templates.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Get custom post type by value?’ is closed to new replies.