WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] query_posts...and/or??? (11 posts)

  1. monkeymynd
    Member
    Posted 4 years ago #

    Hi All,

    I am trying to use query_posts in my index.php to grab posts that are either "sticky" or in a set of categories. Is this possible? Can I somehow use a query instead that would still work with have_posts?

    Currently, I am grabbing all posts that are in a set of categories. While the sticky IS in one of these categories, it is not being included for some reason.

    Any suggestions?

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

  3. monkeymynd
    Member
    Posted 4 years ago #

    Thank you for your reply. I looked at that earlier, but didn't see one that would return posts that are either "sticky" OR in a set of categories. Am I missing it?

    I see just about every other option. Seems that calling query_posts with a "sticky" post parameter only returns "sticky" posts. Yes, you can then further wean them down by adding specific categories, but it will only return posts in those categories if they are "sticky".

    My issue lies in trying to get both. For instance: I have a "sticky" which is in a category called "General" catid #1. I then have several other posts in a category called "Site 1" catid #2. I would like to return all posts that are either in "General" or "Site 1", with "sticky" posts at the top.

    So, I have the following call:

    query_posts(array('category__in' => array(1,2)));

    Problem is, when I do this, my "sticky" posts do not get returned. I get all posts in "General" and/or "Site 1", but not if they are set as "sticky". Any reason why?

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    Try just adding...

    caller_get_posts=0

    I can't say i work with stickies much, but i can test some code if you like..

  5. monkeymynd
    Member
    Posted 4 years ago #

    Adding the above gets doesn't change anything. Still not getting the sticky for whatever reason. The weird thing is, and maybe this will give you a clue, is that if the sticky is the "latest" post, then it brings back the sticky...but, if there are any posts newer that fit the criteria of the categories, then it will just blow of the sticky and not even return it in the list.

    If you do get a change to test this out, I'd appreciate it. I can't imagine what the problem could be.

    This is exactly what I am calling:

    $sitecat_id = get_category_by_slug('mydomain')->term_id;
    $generalcat_id = get_category_by_slug('general')->term_id;
    $args = array('category__in' => array($sitecat_id,$generalcat_id));
    query_posts($args);
  6. monkeymynd
    Member
    Posted 4 years ago #

    Ok, I figured out part of the problem. There is a section of code in the loop that is skipping a category. So, it is actually bringing back the sticky, it's just in another category that is being skipped.

    So, my only remaining problem is that when calling query_posts is does not put the sticky at the top of the order. It puts it in by date order so it's not at the top.

    Again, thanks for your replies.

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    I've been toying around with this, and i've found the problem here, is that whenever you set an array or any category for display you immediately lose the priority of the sticky post...

    I've tried, category__in, cat, category__and... and every which way i could think of..

    Everytime you include posts with stickies AND set the category, stickies seem to lose their order...

    However you can retain sticky priority using a pre_get_posts filter like so...

    In your functions.php (for your theme), plonk this in...

    function setcat_pre_posts($query) {
    	if ($query->is_home) // Assuming you want this to apply when it's your home page
    	{
    		$query->set('cat', '87,88');
    	}
    	return $query;
    }
    add_filter('pre_get_posts', 'setcat_pre_posts');

    Change 87, and 88 to your 2 cat IDs, and remove the code you posted above from your index.php

    NOTE: Regardless of what category the sticky post is in, it should still show up (it did in my testing), and in the correct place(the top).

    If stickies don't show up then add the query_posts line into the index.php with the caller_get_posts parameter like so, you should then see posts from the 2 categories and the sticky (but at the top).

    query_posts('caller_get_posts=0')

    If you wanted to only apply the filter when stickies exist, then you could change the function to..

    function setcat_pre_posts($query) {
    	$testst = get_option('sticky_posts');
    	if($testst[0]) { // If there's first item, then we have stickies
    		if ($query->is_home) {
    			$query->set('cat', '87,88');
    		}
    	}
    	return $query;
    }
    add_filter('pre_get_posts', 'setcat_pre_posts');

    I don't know why this method works exactly, but it does... you get stickies at the top (regardless of category), then posts that follow filtered by the 2 assigned ID's above... :)

  8. monkeymynd
    Member
    Posted 4 years ago #

    Wow...thank you so much! I will give this a shot!

  9. monkeymynd
    Member
    Posted 4 years ago #

    Worked perfectly! Setting topic to resolved :)

  10. monkeymynd
    Member
    Posted 4 years ago #

    Hi t310s_,

    Thought you might see this post. Your code worked perfectly for me...much thanks. But, it seems that my thumbnails are no longer showing when I do this. If you have some time, could you look at this post of mine and see if you see anything that might keep my thumbnails from showing?

    Thanks!

    http://wordpress.org/support/topic/311636

  11. akabir
    Member
    Posted 4 years ago #

    Dear,

    i am so happy to find the following code here,

    function setcat_pre_posts($query) {
    $testst = get_option('sticky_posts');
    if($testst[0]) { // If there's first item, then we have stickies
    if ($query->is_home) {
    $query->set('cat', '87,88');
    }
    }
    return $query;
    }
    add_filter('pre_get_posts', 'setcat_pre_posts');

    this is what i need exactly, but unfortunately it`s not working with me.
    any idea, suggestion why?

    akber

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.