Add random custom posts to the wordpress loop - php

I have a problem Im trying to solve and not having much luck, Iv tried google/stackoverflow etc, but all I find in general 'random post' type answers
here's the issue:
I am getting my posts using a pretty standard loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $cat = get_the_category();
if(strtolower($cat[0]->name) != 'hidden'){
?>
I then display all the post data etc, etc.
I have a custom post type, with an option in the back end to limit the number of these custom posts that can be shown on the homepage
get_option('max_amount')
What I want to do is mix some of the custom posts (custom post type) in with the posts from the loop, not exceeding the 'max_amount' BUT I dont want them next to each other, I need them mixed in.
Hope that all makes sense and someone can help or point me in the right direction
Thanks

If you add the posts to and array you could then use shuffle($posts) to randomise the post. Then use $posts = array_slice($posts, 0, get_option('max_amount') to ensure you don't exceed the max amount.

Related

Wordpress function to target 1, 2-3, 4-5 posts to apply different code/style

I am trying to target the first post, second-third post and fourth-fifth post to achieve a tiled like image effect in Bootstrap and Wordpress. I made a plugin to handle this code and make it a shortcode as well but I get an error on it. I know it could be shortened due to my php knowledge is at beginner level. Any help in shortening it would be appreciated or at least solve the error issue I am having if at all possible. I have the CSS working great but not the php code. I am trying to achieve this.
I started with this code
we can make it simple by add a variable and increase it on loop and check on every time
it will make you control on every post
$i=0;
before
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
then check if it first post like
if($i==0) : echo 'class="active"'
then increase it in every loop
$i++; endwhile; endif;

Different html/css for first 4 posts in the same query in Wordpress

I am trying to change the HTML structure and CSS styles of the first 4 posts in the main WP_Query in archive.php
I am doing this simple thing where I checked the global $wp_query variable.
if ( have_posts() ) :
if( 4 > $wp_query->current_post ) :
the_title();
endif;
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
else :
get_template_part( 'no-results', 'archive' );
endif;
This works fine, the first 4 posts in the query get displayed in whatever HTML/CSS I apply to them before the get_template_part() gets called in.
The problem is when I go to the next page in the pagination, a different set of 4 posts get displayed. The 4 new posts of the second page in the pagination.
I don't want that. I want the same 4 posts that appear on the first page to keep appearing as I go to the next or previous pages. I need to give the first 4 posts a different HTML structure, not just CSS styling and I need them to persist throughout the pagination.
I tried changing the main query with pre_get_posts and using offset but that gave me a set of problems in the theme and the admin panel that I decided against it.
How may I achieve that?
EDIT My first attempt at this problem was to do a second query and leave the main query intact but then I wouldn't be able to check the post_count in the first query to see if it's bigger than 4 because I'm always showing only 4 posts_per_page that's why I need them to be in the same query because I'm going to hide the first 4 posts on the category page that doesn't have more than 4 posts and only show them on the category page that has more than 4 posts.
EDIT 2 To make this simpler to understand, if it's getting too messy.
IF CATEGORY (QUERY) HAS MORE THAN 4 POSTS
DISPLAY 4 POSTS WITH CUSTOM HTML/CSS
THEN GET TEMPLATE PART AND DISPLAY THE REST OF THE POSTS WHILE EXCLUDING THE FIRST 4 POSTS BECAUSE DUPLICATES
ELSE
DISPLAY DEFAULT TEMPLATE PART
Here's a loop I use to show the first four posts, it has a wp_reset_postdata that might be required so your pagination loop is unaffected.
<?php $rp_query = new WP_Query( 'showposts=4' );
if ( have_posts() ) : while ( $rp_query->have_posts() ) : $rp_query->the_post(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<? endif; ?>
Solution is already built-in and available as plugin.
Please try Blog Designer PRO plugin - https://codecanyon.net/item/blog-designer-pro-for-wordpress/17069678?ref=miyanialkesh7
Best regards,
Alkesh

Add custom posts to search (separately to categories) in Wordpress

I have a search results page under construction, and it splits the posts out in to categories (products, recipes, articles), in 3 separate sections down the page. The categories (recipes and articles) were fine and easy - creating a query to use in 2 separate loops, but I'm having trouble with the custom post type.
I want to pull these in with the loop as well if possible, but not sure whether to do it on the post type or taxonomy. The products are obviously split in to sub categories under the taxonomy, so when I tried it, it was pulling the same product multiple times.
Right now, having given up on trying to pull them through using the taxonomy, I am trying this:
<?php
$args = array(
'post_type'=> 'products'
);
$products = new WP_Query($args);
if ( have_posts() ) :
?>
<div class="product suggestions cfx sub-range">
<h2>We found xx products...</h2>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
This code isn't pulling anything through...will a different query work, or should I still be trying to use the custom taxonomy?
When I search for a solution, pretty much everything I can find is creating a search.php page for displaying only custom post types, or just adding the custom post type to search results, not having a separate loop for custom post types along side the loops for categories...
Thanks
I think I got it. I changed the slug from products and it started working for some reason. So now i have this:
<?php
$args = array ('post_type'=>'product-ranges');
$products = new WP_Query($args);
if ( have_posts() ) :
?>
<div class="product suggestions cfx sub-range">
<h2>We found xx products...</h2>
<?php while ( $products->have_posts() ) : $products->the_post();
?>

max_num_pages returning number of posts instead of number of pages

I am using the PBD Load Next Post plugin and it's working great, but I can't figure out why the max_num_pages is returning 44 (number of posts in that category) instead of 4 (number of pages it should be). Here are all the codes that should be relevant:
http://pastebin.com/ezAbD2eH
Here is the page that it is running on: garthreckers(dot)com/category/united-states/ (sorry, not enough rep to post another full link)
It's also running on the Europe page but its returning 20 over there (number of posts again).
Also, the if(mobile...) is from the mooble plugin if that makes a difference. I have tried stripping out the code for that and deactivating and it still doesnt work.
Any help will be great since I have been trying to fix this problem for 3 days now with no success.
I know this may not be the best way but if you want to you can change the default number in the javascript as a workaround
Your JS http://pastebin.com/ezAbD2eH
Line 58:
var max = parseInt(pbd_alp.maxPages);
Change to:
var max = parseInt(pbd_alp.maxPages/12); //divide it by the number of posts per page.
if you want you can probably set that as a variable and use it elsewhere where your $arg is as well so that the number is set globally. Set $posts_per_page = 12 inside your PHP section.
var max = parseInt(pbd_alp.maxPages/<?php echo $posts_per_page; ?>);
I may have found your issue. Currently in your PHP code when displaying all your posts you have a query that calls for Categories
wp_list_categories('show_option_none=&orderby=name&show_count=1&hide_empty=1&use_desc_for_title=1&child_of='.$cat.'&title_li=');
Currently this is throwing off the number as it is showing max_num_pages based on the category list number. I would change the way you are displaying posts so that it is not using wp_list_categories to retrieve them.
Reference: http://codex.wordpress.org/Template_Tags/get_posts
<?php
$args = array( 'numberposts' => 3 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endforeach; ?>

How do I add a custom key to PHP in Wordpress

I am attempting to make a custom query in Wordpress. I want to have it pull up any posts that are tagged relevant to the page that people are on. IE - If they are on the Bay 198 Skatepark page, they also see a list of all Bay 198 Skatepark posts.
I've gotten the custom query to work, but I need this to be dynamic so that if you are on the Bay 198 page, you see those posts, but if you are on the Precision Skateboards page, you see those specific posts.
The easiest way I could think to accomplish this was to create a custom field on each page that would be the same as the tag and that way I would just fill in the tag for each page as a custom field and it would return the posts.
The issue is that I can't figure out how to implement the custom key call into the custom query. Here's my code so far, this one being the one that works:
<?php $second_query = new WP_Query(' tag=bay-198&posts_per_page=500 '); while( $second_query->have_posts() ) : $second_query->the_post(); ?>
I just need to figure out how to replace that "tag=bay-198" and have it dynamically pull in the tag based on the page.
Hopefully that makes sense, I appreciate any help.
You can use a variable that containts the value of your custom field. Then add that variable into the query like so:
<?php
$mykey_values = get_post_custom_values('my_key');
$tag = $mykey_values['your-custom-field'];
$second_query = new WP_Query(' tag=' . $tag . '&posts_per_page=500 '); while( $second_query->have_posts() ) : $second_query->the_post(); ?>

Categories