• Resolved frankindelicato

    (@frankindelicato)


    Restricting the front page to just one category by placing the following before “The Loop” by using:

    <?php //if (is_front_page()) { query_posts(“cat=1”); } ?>

    or during The Loop by using:

    <?php if (is_front_page() && ! is_category(‘1’))contine; ?>

    breaks the “Sticky Posts” from appearing at the top.

    In the function &get_posts() wp-includes/query.php in the section that is commented as:

    // Put sticky posts at the top of the posts array
    there is a line of code:

    if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q[‘caller_get_posts’] ) {

    if the $this->is_home test is removed the sticky post work fine. The new line of code should read:

    if ( $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q[‘caller_get_posts’] ) {

    This is a possible fix, because I still do not understand why the is_home test fails. I still need to understand why sticky post are restricted to the home page anyway. Does anyone know the strict definition of the home page? How does the home page get set during runtime? Any other advise would help.

    Thanks

Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter frankindelicato

    (@frankindelicato)

    After posting the above “possible solution” to the sticky issues I found that after removing the “is_home” test I discovered that I then had the “sticky posts” showing up from category “My Category” inside “My Other Category”. Sticky’s were really Sticky within each category. I want the Sticky’s to work in an intuitive way. I make Sticky’s in one Category they should only show up on that category, and I should be able to have separate Sticky’s on each Category. Anyway, in the same code mentions above “query.php” there is another section of code that starts with the comment:
    // Fetch sticky posts that weren’t in the query results
    This block of code adds the sticky’s to every query. That I don’t want, so I just commented out that code and now Sticky’s work in the way I expected. At least for now….

    You might start by fixing the first 2 pieces of code you’ve posted.

    if (is_front_page()) { query_posts("cat=1"); }

    Now you have no query vars on the front page, except the category, it’s hardly surprising you get wrong data sets when you do things like this, i see this quite alot though..

    if (is_front_page() && !is_category('1'))contine;

    Inside the loop you should be using in_category, not is_category, and using numeric values not strings, ie. in_category(1), not in_category('1') .. same applies for is_category, but it doesn’t matter here anyhow because the usage is incorrect..

    Thread Starter frankindelicato

    (@frankindelicato)

    Thank you for the advice.
    The first piece of code was suggested by another developer on this forum and works perfectly. Without it I get a mixture of posts on the home page from the two different categories of posts I have. The second piece does not work at all whether you use is_category or in_category.
    The main problem I had once the correct category was being displayed was that the sticky post would not work and the changes I made (sited earlier in this post) to query.php fixed the sticky issue. Now I get the correct category posts on the correct pages with the correct sticky posts for each category.
    I am going to mark this thread as resolved because everything is working fine now.

    Ok slow down a little, i think you’re skipping past something important here, and that’s the initial problem..

    Are you saying your main page uses 1 category, and additionally to that you have sticky posts, that may or may not be in that category, and should only be present if they are in that category alone, and not otherwise? Is this the initial problem you’re trying to fix?..

    Thread Starter frankindelicato

    (@frankindelicato)

    Hi t3l01,

    Yes. I currently have two categories, each with a sticky post. The front page (home page) always showed both of the stickies.

    Can you tell me (or show me) how you’re filtering posts to only using one category on the home/front page…

    — Mark is fine, if it’s easier to spell.. 😉

    Thread Starter frankindelicato

    (@frankindelicato)

    Hi Mark,

    I didn’t like the way the home page worked by showing all the categories so I modified the Whitehouse theme and in it’s _posts.php file I added:

    <?php if (is_front_page()) { query_posts($query_string.'&cat=1');} ?>;

    I added the query_string after reading your initial response to this thread, and realizing the possible error of not including it.

    Ok, and if you were to change that line to read..

    <?php if (is_front_page()) { query_posts( 'cat=1' ); } ?>;

    …does the page then behave as expected?

    It works here, only get stickies for the right category..

    However, that’s no solution for keeping hold of other query vars, so use this instead..

    <?php
    if( is_front_page() ) {
    	//$args = array( 'cat' => 1 );
    	// Query parameters here
    	$args = array( 'category__in' => array( 1 ) );
    	// Checks for existing query vars and merges if necessary
    	$args = ( $wp_query->query && !empty( $wp_query->query ) ) ? array_merge( $wp_query->query , array( $args ) ) : $args;
    	// The query
    	query_posts( $args );
    }
    ?>

    Hope that helps..

    Thread Starter frankindelicato

    (@frankindelicato)

    Thank you Mark, that works great.
    I have another question — Sorting the posts seems to ignore the sticky post, it always puts it according to its natural date. Is there a way to sort and still have it be first?

    Yes i believe that will happen when you define the category, see the thread here.
    http://wordpress.org/support/topic/238693

    It seems the problem querying a set category and sticky posts still exists, you can confirm this by removing the category parameter. In testing, removal of the category parameter did indeed re-order and fix the sticky position.

    There’s also a trac ticket for this issue(or related) here.
    http://core.trac.wordpress.org/ticket/10549
    This could possibly be related to.
    http://core.trac.wordpress.org/ticket/9978

    I think i’ve found a way to work around the problem though, it’s not as elegant as the previous code, but it doesn’t involve any additional queries, so there’s no downside other then having to use different code..

    I don’t use a front page setup, so the code differs for your usage slightly..

    function frontpage_query( $query ) {
    	if( $query->is_front_page ) {
    		$query->set( 'category__in' , array( 1 ) );
    		$query->set( 'caller_get_posts' , 0 );
    	}
    	return $query;
    }
    add_filter( 'pre_get_posts' , 'frontpage_query' );

    ..where as in my testing, the above used..

    if( $query->is_home )

    .. it might make a difference, so i’m just pointing this out in advance..

    Add that code to your theme’s functions.php, then get rid of the query_posts and code from before, and just put this inside your loop.. (after the while( have_posts( … before the HTML etc..)

    if( is_front_page() ) {
    	if( !in_category(1) ) continue;
    }

    The function first sets the category and sets caller_get_posts to 0, which basically means stickies at the top…

    .. the condition then catches other sticky posts that seem to crop up in the results incorrectly, so continue basically instructs the loop to skip that result..

    End result – should be posts in set category, with stickies AT THE TOP… 🙂 if all goes to plan… 🙂

    Thread Starter frankindelicato

    (@frankindelicato)

    Thank you! Your a Genius!

    Happy to help..

    Did the code work out ok, shall i take it you’ve tested the code, just to clarify, for others that may be reading.

    🙂

    Thread Starter frankindelicato

    (@frankindelicato)

    Yes, I tested it and it works fine. It did not make a difference whether I used is_frontpage or is_home in your code. I learned a lot about WordPress in this exercise. Thank you.

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Possible Fix for Sticky Problems in function &get_posts()’ is closed to new replies.