WordPress.org

Ready to get started?Download WordPress

Forums

Types - Custom Fields and Custom Post Types Management
[resolved] How to use types custom fields in query ? (14 posts)

  1. Tarun
    Member
    Posted 2 years ago #

    Is it possible to use types custom field in a WordPress query. I have been trying to use a custom field 'featured' created by types, to show featured posts in a custom post-type. I have tried the following but none worked:

    query_posts( array(
    'post_type' => 'content',
    'posts_per_page' => 3,
    'paged' => get_query_var('page'),
    'meta_key' => 'featured',
    'meta_value' => 1
    ) );

    AND

    query_posts( array(
    'post_type' => 'content',
    'posts_per_page' => 3,
    'paged' => get_query_var('page'),
    'featured' => 1
    ) );

    Any suggestions.

    Thanks for your help!

    http://wordpress.org/extend/plugins/types/

  2. AmirHelzer
    Member
    Plugin Author

    Posted 2 years ago #

    Have you considered trying Views?

    If you're not 100% familiar with WordPress API, you'll find this functionality, and a lot more, included in Views. You'll be able to filter by content types, custom fields, taxonomy and anything else you need.

    http://wp-types.com/home/views-create-elegant-displays-for-your-content/

  3. Tarun
    Member
    Posted 2 years ago #

    I don't want to overwhelm the client with so much functionality for a simple featured post.

    Anyways I found the solution!

    Types was adding "wpcf-" in front of every custom field name in the database. So i changed my code to the following and it worked.

    query_posts( array(
    'post_type' => 'content',
    'posts_per_page' => 3,
    'paged' => get_query_var('page'),
    'meta_key' => 'wpcf-featured',
    'meta_value' => 1
    ) );

    Hope this helps someone.

    Thanks for the reply.

  4. jnhghy
    Member
    Posted 2 years ago #

    Hi, Yes it is possible, the fallowing code should do the job:

    $args = array('meta_query'=>array(array('key' => 'featured', 'value' => 1)),'posts_per_page' => 3,'post_type' => 'content', 'paged'=>get_query_var('page'));
    query_posts($args);

    This should work if you have saved a custom field "featured" or replace "featured" with "wpcf-featured" if that's the custom post type that will give you the desired posts, with the value 1 for the featured posts, by default WordPress is not saving that information as custom field and to get featured posts you need to use the fallowing query:

    $sticky = get_option( 'sticky_posts' );
    $args = array('posts_per_page' => 3,'post__in'=>$sticky,'post_type' => 'content', 'paged'=>get_query_var('page'));
    query_posts($args);

    Let me know if you have troubles implementing the code; -> Hope it works out of the box!

    Regards

  5. Tarun
    Member
    Posted 2 years ago #

    Thanks jnhghy this query style works too.

  6. Cristiano
    Member
    Posted 2 years ago #

    I tried three ways and could not get it working...

    'meta_key' => 'mymeta',
    'meta_value' => 1
    
    'meta_key' => 'wpcf-mymeta',
    'meta_value' => 1
    
    'key' => 'mymeta',
    'value' => 1

    What am I should doing wrong?
    Thanks

  7. Tarun
    Member
    Posted 2 years ago #

    'meta_key' => 'wpcf-mymeta',
    'meta_value' => 1

    This should work.. Could you write your complete query here. Maybe something else is wrong.

  8. Cristiano
    Member
    Posted 2 years ago #

    Hi Tarun, thanks for your help!

    This is my query:

    $args = array(
                        'post_type' => 'colabora',
                        'order' => 'desc',
    		    'meta_key' => 'wpcf-parceiros',
    		    'meta_value' => 1
                    );
    
                    query_posts( $args );
                    while ( have_posts() ) : the_post(); ?>
    
                        <?php $postid = get_the_ID(); ?>
    
                        <li class="cada_colabora">
                        	<?php if ( function_exists( 'get_the_image' ) ) {
    			get_the_image( array( 'meta_key' => array( 'Thumbnail', 'thumbnail' ), 'size' => 'full' ) );
    			} ?>
                        </li>
    
                    <?php endwhile;
                    // Reset Query
    		wp_reset_query(); ?>

    I really don´t know whats wrong. Maybe something on the Types admin...
    Thanks!

  9. Tarun
    Member
    Posted 2 years ago #

    Hi Cristiano,

    Code that you are using seems fine. If you are using types custom fields to upload the image, try adding that custom field name to get_the_image function. So if the fieldname created by types is 'testfield' the code would be:

    <?php if ( function_exists( 'get_the_image' ) ) {
    	get_the_image( array( 'meta_key' => array( 'Thumbnail', 'thumbnail', 'wpcf-testfield' ), 'size' => 'full' ) );
    } ?>
  10. Cristiano
    Member
    Posted 2 years ago #

    Hi Tarun,

    No, I´m not using Types custom fields for the image. Its a featured image of the default wordpress. And the problem is not with the image, because when I take out the "meta" conditionals, it works fine.

    I´m using Types custom fields to "categorize" the post type "colabora". Maybe I should use categories for that job and its done.

  11. Cristiano
    Member
    Posted 2 years ago #

    Tarun, I erase the custom field, begin again with another slug, and now it works... mysteries of php... :)

    How you said, this works:

    'meta_key' => 'wpcf-mymeta',
    'meta_value' => 1

    Thanks!

  12. Tarun
    Member
    Posted 2 years ago #

    Glad it worked! :)

  13. saminternetmarketing
    Member
    Posted 1 year ago #

    Hi, I ran into a similar kind of problem. I want to display the images of my custom field category wise. The name of the category is product and the name of the custom image field is product_image. I am using the below code in my category template file. But nothing is working out.

    <?php
    $args = array(
                        'post_type' => 'products',
                        'order' => 'desc',
    		    'category' => 'tg800-cat-6-a-copper-utp'
                    );
    
                    query_posts( $args );
                    while ( have_posts() ) : the_post(); ?>
    
                        <?php $postid = get_the_ID(); ?>
    
                        <li class="cool-image">
                        	<?php if ( function_exists( 'get_the_image' ) ) {
    			get_the_image( array( 'meta_key' => array( 'Thumbnail', 'thumbnail', 'wpcf-product_image' ), 'size' => 'full' ) );
    			} ?>
                        </li>
    
                    <?php endwhile; ?>
    
    		<?php wp_reset_query(); ?>

    Am I doing something wrong here? Thanks...

  14. saminternetmarketing
    Member
    Posted 1 year ago #

    Wow!! I never knew where that function get_the_image() was coming from. I thought it might be a WP function. That is what makes the magic happen!!! I installed the get the image plugin and voila the same code above worked like a charm. Thanks a lot guys for the discussion. It helped me a lot, otherwise I would have wasted a few more nights of my sleep like a fool.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic