WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP 2.6 get_posts (12 posts)

  1. Anonymous
    Unregistered
    Posted 6 years ago #

    I have 2.6 WP version. Trying to get posts from one category. Using code

    $args = array(
    				'post_type' => 'post',
    				'category' => $cat->cat_ID,
    				'offset' => 1,
    				'numberposts' => 2,
    				'post_status' => 'publish',
    				'orderby' => 'date',
    				'order' => 'DESC',
    				'meta_key' => 'video',
    				'meta_value' => 1);
    			        $posts = get_posts($args);
    				foreach($posts as $post)
    				{
    					setup_postdata($post);
    ...

    And getting all posts from $cat->cat_ID and child cats. Any ideas how to fix?

    query_posts is useless, since it havent support of meta_key&meta_value

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    query_posts does indeed support meta_key and meta_value in 2.6. So does the WP_Query method.

    In fact, get_posts is just using WP_Query now. It's no longer any different. So switch already.

  3. Anonymous
    Unregistered
    Posted 6 years ago #

    Hm, how then i should use it?
    Such code doesnt work:

    query_posts('cat=1&metakey=video&meta_value=1&post_type=post&offset=1&numberposts=2&post_status=publish&orderdy=date&order=desc');
    				while(have_posts())
    				{
    					the_post();

    Anyways, i know how to fix it to make work for me.

    wp-includes/query.php
    line 1022:
    $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));

    Just comment it and you will get directly what you asked for.

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    "metakey" is not the same as "meta_key". Spelling is important here...

    Also, using category__and instead of cat might be more what you're looking for. Better than hacking the core code.

  5. Anonymous
    Unregistered
    Posted 6 years ago #

    well. Good idea, but still not work:

    query_posts('cat='.$cat->cat_ID.'&meta_key=video&metavalue=1&post_type=post&offset=1&showposts=2&post_status=publish&orderdy=date&order=desc');
    				while(have_posts())
    				{
    					the_post();

    Such code still give me cat and subcats posts.

  6. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Yes, because the "cat" parameter gives the children as well.

    And you misspelled "meta_value" this time.

    Did you read what I posted about using category__and instead? Might be worth looking closer at that.

    Also, the array thing you were doing earlier with get_posts will work just as well with query_posts too. No need to do all that string work.

  7. Anonymous
    Unregistered
    Posted 6 years ago #

    Thank you man for your help.

    Well, meta_value just was misspelled here, it works now.
    well, such code gives me 2nd and 3rd record i belive:

    $args = array(
    				'post_type' => 'post',
    				'category' => $cat->cat_ID,
    				'offset' => 1,
    				'showposts' => 2,
    				'post_status' => 'publish',
    				'orderby' => 'date',
    				'order' => 'DESC',
    				'meta_key' => 'video',
    				'meta_value' => 1);
    
    				query_posts($args);

    Something else i can do?

    And even 'category__and' & 'category__' gives same (2nd and 3rd record).

  8. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    'category' => $cat->cat_ID,

    ...

    Did you even try 'category__and' => array($cat->cat_ID) at all?

  9. Anonymous
    Unregistered
    Posted 6 years ago #

    Wow. It finally works. Big thanks for you!

  10. johnstonphilip
    Member
    Posted 5 years ago #

    Does anyone know where the function for $args sits? I am trying to figure out how to get the images/attachments to order based on the order I give them in the admin.
    Currently they remain in the order they were uploaded even if I re-order them by dragging and dropping in the admin.

    I am wondering if it has something to do with ORDER BY in the mySQL call - and perhaps that call is done somewhere in a function related to $args?

  11. johnstonphilip
    Member
    Posted 5 years ago #

    maybe it has something to do with this:
    attachment->menu_order

    About a minute ago it WAS showing the order of the images as they are in the admin. Now they just show up as 0 for every post. I can't see anything that has changed since.

  12. johnstonphilip
    Member
    Posted 5 years ago #

    Got it!

    I used a for loop and then checked against current Post Number using attachment->menu_order

    if ($currentAttachmentNum == $attachment->menu_order){

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags