Support » Plugin: Carbon Fields » Use get_posts and order by carbon fields

  • Resolved joelschmid

    (@joelschmid)


    Hey guys!

    Is there a way to order the posts, which I get with the _get_posts()_ function, depending on a carbon field?

    `
    $args = array( ‘posts_per_page’ => 5, ‘offset’=> 0, ‘category’ => $settings->tax_post_category, ‘orderby’ => ‘crb_ws-id’, ‘order’ => ‘DESC’ ); ?>
    $myposts = get_posts( $args );

    `

    Thank you in advance for your help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Hi @joelschmid,

    Let me know if this is the correct situation you have:
    1. You have the crb_ws-id field added to all posts
    2. You wish to pull all posts and order them based on the value they have in their crb_ws-id field

    To achieve the above use this:

    `
    $args = array(
    ‘posts_per_page’ => 5,
    ‘offset’ => 0,
    ‘category’ => $settings->tax_post_category,
    ‘meta_key’ => ‘_crb_ws-id’, // NOTE: don’t forget the leading underscore – meta keys are stored with an underscore prepended to the field name
    ‘orderby’ => ‘meta_value_num’, // NOTE: change to ‘meta_value’ if the values in crb_ws-id are not numeric
    ‘order’ => ‘DESC’,
    );
    $myposts = get_posts( $args );
    `

    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Hi @joelschmid,

    Let me know if this is the correct situation you have:
    1. You have the crb_ws-id field added to all posts
    2. You wish to pull all posts and order them based on the value they have in their crb_ws-id field

    To achieve the above use this:

    
    $args = array(
    	'posts_per_page' => 5,
    	'offset' => 0,
    	'category' => $settings->tax_post_category,
    	'meta_key' => '_crb_ws-id', // NOTE: don't forget the leading underscore - meta keys are stored with an underscore prepended to the field name
    	'orderby' => 'meta_value_num', // NOTE: change to 'meta_value' if the values in crb_ws-id are not numeric
    	'order' => 'DESC',
    );
    $myposts = get_posts( $args );
    
    Plugin Author htmlBurger

    (@htmlburger)

    Closing this in favor of your GitHub issue here:
    https://github.com/htmlburger/carbon-fields/issues/219

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Use get_posts and order by carbon fields’ is closed to new replies.