last day (14 days later) » 

5:05 PM
OK, I'm here, you can chat
 
Hello. Thanks for coming.
in my index.php file
there is this statement
 
Have you tried my solution, just a hint, that code goes into functions.php
 
But
would it still work if the main query is modified?
I thought main query would override it
 
How is it modified
 
It lists only CPT
for taxonomy pages anyway
I might be sounding pretty noobish I am aware of it.
 
5:10 PM
What templates do you have because I do not thing index.php is really used here
 
ooopss I got confused I didn't mean that one. taxonomy-product-category.php
I am trying your solution right now
 
Crap, sorry, I actually missed that to. How does your main loop looks like
 
Is it okay where to find it?
Actually
Your method worked perfectly
But still if it is okay, I want to see if main query or main loop is altered in a way so that it can create
a drop in SEO
in the index.php I added the statement 'orderby=rand&order=ASC'. to the wp_query parameters to achieve randomization in the main page
 
pre_get_posts is the correct way to alter the main query. pre_get_posts alters the variables passed to WP_Query (which is used by the main query) before the main query executes the generated SQL. There are no extra queries or loss in time, although you must realise that random ordering is much slower than normal ordering
That is wrong, you should do the same as what I have showed you.
 
@PieterGoosen I am making the website for my mom to sell the handmade stuff for children. So I want the ads to be random. Therefore, I have to sacrifice the performance or normal order for the sake of functionality of the website. It is a good information to know that though. Thank you.
 
5:20 PM
In the code in my answer, change && $q->is_tax( 'product-category' ) to && ( $q->is_tax( 'product-category' ) || $q->is_home( ) )
Then in index.php go back to the main loop as in your taxonomy page
This will increase performance dramatically
 
@PieterGoosen Thank you so much. I am applying the modifications right now
 
I see you have a WordPress Development account, go and read my post there which I have done. It should be really helpful to someone that is still new. Here is the link wordpress.stackexchange.com/a/155976/31545
it might be a bit much in the beginning, but there are some good point and links there on where to use what
 
@PieterGoosen I will ASAP. Really, few people are as helpful as you are to noobs on SO. People are angry haha. For some reason, the latest addition you suggested caused a syntax error. I probably did something wrong. I am fixing it
 
I will quickly test as well on my side
Works on my side. Here is the full code
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() // Only targets the front end
&& $q->is_main_query() // Only targets the main query
&& ( $q->is_tax( 'product-category' )
|| $q->is_home()
)
) {
$q->set( 'orderby', 'rand' );
}
});
Soory for no indentation, formatting gets stripped in chat seems to me
 
Haha it is okay. I can reformat it
I think I have corruputed my functions.php by mistake
But I have backup
implying in 1 minute
 
5:32 PM
I hope you are testing on a local test install
 
No I test on the actual page
Why?
It is pretty new
Has no visitors as of now
It doesn't work on main page
weird
 
local test installs are a pleasure, you can play around, test code, mess it up and reinstall and so on without touching the live server. If you make a silly syntax mistake, you can simply correct it, no need to log in via ftp to repair the corrupt file
How does your loop look like
 
@PieterGoosen On the index.php?
<?php
$home_count = get_option('zens_home_count');
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page='.$home_count.'&post_type=products'.'&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
I don't have any other templates to modify the mainpage so it falls back till index.php
 
OK, you need to remove that. Just a note, the method you are using there by using $wp_query as a local variable and nulling it is exactly what query_posts does, it breaks the main query object. You should never ever do that. If you really need a custom query, rather do $query = new WP_Query() and not $wp_query = new WP_Query()
Anyways, replace that with the following
if ( have_posts() ) {
while ( have_posts() ) {
the_post();

// Whatever is in your loop

}
}
 
@PieterGoosen sigh... I will never ever buy from those guys
@PieterGoosen But shouldn't it load all types of posts and not just the CPT?
 
5:42 PM
Some people dish up a lot of crap and then sells it to unknowing people, they get their money and never hear from them again
AH, you have custom post types as well. OK, your pre_get_posts action which I posted in my answer, go back to that one.
Lets first sort out your index.php
 
@PieterGoosen Yes sir!
 
Copy your complete index.php and paste it here
 
<?php get_header(); ?>
<?php
if(get_option('zens_home') == "blog") { ?>
<?php include (TEMPLATEPATH . '/lib/blog-home.php'); ?>
<?php } else { ?>
<div id="home-content" class="clearfix">
<ul id="shelf">

<?php
$home_count = get_option('zens_home_count');
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page='.$home_count.'&post_type=products'.'&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<li class="box" id="post-<?php the_ID(); ?>">
 
Replace all that with the following
<?php get_header(); ?>
<?php
if(get_option('zens_home') == "blog") { ?>
<?php include (TEMPLATEPATH . '/lib/blog-home.php'); ?>
<?php } else { ?>
<div id="home-content" class="clearfix">
<ul id="shelf">

<?php
?>
<?php while (have_posts()) : the_post(); ?>

<li class="box" id="post-<?php the_ID(); ?>">
<?php $disc = get_post_meta($post->ID, 'fabthemes_discount', true); ?>
<?php if ( $disc ) { ?>
<span class="salebadge"></span>
<?php }?>

<a href="<?php the_permalink() ?>"><img class="productshot" width="172" height="243" src="<?php get_image_url(); ?>" alt="<?php the_title(); ?>"/></a>
You should just indent it properly again
 
Done. Right now, it just shows normal posts and not the CPT
 
5:52 PM
Great. now you add the following in functions.php
add_action( 'pre_get_posts', function ( $q )
{
if ( $q->is_home()
&& $q->is_main_query()
) {
// Set posts_per_page
$q->set( 'posts_per_page', get_option('zens_home_count') );
// Set custom post type
$q->set( 'post_type', 'products' );
// Set random ordering
$q->set( 'orderby', 'rand' );
}
});
 
instead of the old code we added to functions.php ?
 
The code from my answer should stay as well, make sure to revert the modification we did earlier back to the exact code in my answer
 
@PieterGoosen Aye Aye! implying it right now
 
This should solve your homepage issue
 
four words
works
like
a
charm
I really can't thank you a lot probably
 
6:00 PM
Great. no problem. Glad it all worked out
 
@PieterGoosen I am really so thankful for your time and patience! Will read the post you posted too!
Thanks so much!
 
You should enjoy the rest of your night (or day depending on where you are in the world) and good luck with the website
 
Thanks a lot!!
 
Cheers
 

last day (14 days later) »