WP_Query orderby 'rand' not working - php

Trying to order some posts I'm displaying on a single custom post type page with random, but they aren't random at all. :/
<?php
// Grab the taxonomy term slug for the current post
$terms = get_the_terms( get_the_ID(), 'category-staff' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->slug;
}
$on_draught = join( ", ", $draught_links );
?>
<div class="container hidden-xs">
<div class="row">
<div class="col-sm-12">
<hr />
<h3 class="text-center">Other People At Our Great Resort</h3>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-lg-10 col-lg-offset-1">
<div class="row staff-list">
<?php
// WP_Query arguments
$args2 = array (
'post_type' => 'staff',
'tax_query' => array(
array(
'taxonomy' => 'category-staff',
'field' => 'slug',
'terms' => $on_draught,
),
),
'nopaging' => false,
'posts_per_page' => '4',
'order' => 'DESC',
'orderby' => 'rand',
);
// The Query
$query2 = new WP_Query( $args2 );
// The Loop
if ( $query2->have_posts() ) {
while ( $query2->have_posts() ) {
$query2->the_post(); ?>
<div class="staff staff-other col-sm-3 text-center">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo get_the_post_thumbnail( $_post->ID, 'large', array( 'class' => 'img-responsive img-circle img-staff' ) ); ?>
<h4><?php the_title(); ?></h4>
<?php if (get_field('staff_job')) { ?>
<p><?php the_field('staff_job'); ?></p>
<?php } ?>
</a>
</div>
<?php }
} else { ?>
<?php }
// Restore original Post Data
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
<?php endif; // terms if statement ?>

Turns out it was something to do with WPEngine. They disable rand() from the server and it needs to be enabled manually.

Another solution may be to add this code before running the new WP_Query($args) function.
remove_all_filters('posts_orderby');
https://developer.wordpress.org/reference/functions/remove_all_filters/

Related

get specific custom type post in specific category - wordpress

I have a problem, i have a custom type post (authors) and taxonomy (manager and team) called position
I need to get all authors of the team on a page and need the manager to be at the first one of them
the authors ordered by name but the manager name begin with "T"
what can I do with this situation.
my code is
<?php
/**
* The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
* Template Name: taxonomies
*
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
get_header(); ?>
<?php
// echo the $slug name of the page
$page_names = get_the_title(); ?>
<section id="articles" class="articles">
<div class="<?php echo $classcase ?>">
<?php
$loop = new WP_Query( array( 'post_type'=>'authors','posts_per_page' => '-1','orderby' => 'title','order'=>'ASC' ) );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</section>
<div class='endauthors'></div>
For this task you need to use tax_query https://developer.wordpress.org/reference/classes/wp_tax_query/
First we take all managers sorted by title, then the entire team
Try this code
<?php
/**
* The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
* Template Name: taxonomies
*
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
get_header(); ?>
<?php
// echo the $slug name of the page
$page_names = get_the_title(); ?>
<section id="articles" class="articles">
<div class="<?php echo $classcase ?>">
<?php
$args = array(
'post_type' => 'authors',
'posts_per_page' => '-1',
'orderby' => 'title',
'order'=>'ASC'
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => array( 'manager' )
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
<?php
$args = array(
'post_type' => 'authors',
'posts_per_page' => '-1',
'orderby' => 'title',
'order'=>'ASC'
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => array( 'team' )
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</section>
<div class='endauthors'></div>

ACF row to show thumbnails of recent posts

I'm trying to create a ACF flexible content row to display the most recent post thumbnails for a given category. However it keeps throwing a critical error and I'm not sure why.
<?php
$section_id = get_sub_field('section_id')
$categories = get_sub_field('categories');
$tags = get_sub_field('tags');
$postnum = get_sub_field('number_of_posts');
if (!is_array($categories)) {
$categories = array($categories);
}
$tags = get_field('my_tags_field');
if (!is_array($tags)) {
$tags = array($tags);
}
$args = array(
'post_type' => 'post',
'numberposts' => $postnum,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => $categories
),
array(
'taxonomy' => 'post_tag',
'terms' => $tags
)
)
);
$query = new WP_Query($args);
?>
<style>
</style>
<section class="post_row_with_thumbnails" id="<?php echo $section_id; ?>">
<div class="container-fluid">
<div class="row">
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<div class="col">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>" class="project_pics">
<h5 class="posttitle"><?php the_title(); ?></h5>
<h6 class="postdate"><?php the_date(); ?></h6>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
</div>
</section>
I have tried substituting WP_Query() with get_posts() but it gives me the same critical error.
In line 2 the ; at the end of the line, in this part:
$section_id = get_sub_field('section_id') ; THE DOT AND COMMA IS MISSING
This is the reason for the critical error.

Dispaly posts by different category in 1 page - Wordpress Development

I have created custom post type, and I am dispalying posts in archive page, but I wanna display posts by different categories in 1 single page. Like this:
But my posts are currently like this
How can i achieve that? I have searched a lot but didn't find any solution to do this. So, that's why I am putting question here.
Here is my code:
<section class="careerBlogs">
<div class="container">
<div class="row with-gutters">
<?php
$args = array (
'post_type' => array( 'career' ),
'post_status' => array( 'publish' ),
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
);
$templates = new WP_Query( $args );
if ( $templates->have_posts() ) {
while ( $templates->have_posts() ) {
$templates->the_post(); global $post;
$customVars = get_post_meta($post->ID, 'custom_vars', true);
if(!empty($customVars)){
$isRemotely = $customVars['remotely'];
}
// Categories
$categories = get_the_terms( $post->ID, 'career_category' );
foreach( $categories as $category ) { ?>
<div class="col-xs-12">
<div class="careerBlogs--title">Open Positions in <?= $category->name; ?></div>
</div>
<?php } ?>
<div class="col-xl-12">
<div class="card mb-4">
<a href="<?= get_the_permalink(); ?>" class="card-link">
<div class="card-info">
<div class="card-title"><?= get_the_title(); ?></div>
<div class="location">
<span>Lahore</span>/<span><?= $isRemotely ? 'Remote' : '' ?></span>
</div>
</div>
</a>
</div>
</div>
<?php } } else {
echo 'no posts to show';
}
wp_reset_postdata();
?>
</div>
</div>
</section>
It is in my archive-career.php page. Can you please help me to achieve that? I am stuck here
After struggling a lot, I have solved my problem with this:
<?php
// I get my Categories
$categories = get_terms('career_category' );
$currentCatName = '';
foreach( $categories as $category ) { ?>
<div class="row with-gutters">
<div class="col-xs-12">
<!-- Assiging category name -->
<div class="careerBlogs--title">Open Positions in <?= $category->name; ?></div>
</div>
<?php
$args = array (
'post_type' => array( 'career' ),
'post_status' => array( 'publish' ),
// here with 'tax_query' i solved my problem to show posts
// by categories (not to show double category names)
'tax_query' => array(
array(
'taxonomy' => 'career_category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$templates = new WP_Query( $args );
if ( $templates->have_posts() ) {
while ( $templates->have_posts() ) {
$templates->the_post(); global $post;
?>
<div class="col-xl-12">
<div class="card mb-4">
<a href="<?= get_the_permalink(); ?>" class="card-link">
<div class="card-info">
<div class="card-title"><?= get_the_title(); ?></div>
<div class="location">
<span>Lahore</span>/<span>Remote</span>
</div>
</div>
</a>
</div>
</div>
<?php } } else {
echo 'no posts to show';
} ?>
</div>
<?php }
wp_reset_postdata();
?>
See:

Exclude specific Woocommerce product category term(s) from a WP Query

I'm trying to load latest product in Woocommerce (on home page). I want to exclude specific product category from display on the loop, but exclude product category id is not working for me.
What I have done so far:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'post_status' => 'publish',
'taxonomy' => 'product_cat',
'exclude' => 29,//exclude mention category id from loop
'parent' => 0
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
<div class="popular-inner">
<a href="<?php echo get_permalink(); ?>">
<div class="popular-image d-flex ">
<div class="align-self-center mx-auto ">
<?php the_post_thumbnail(); ?>
</div>
</div>
<div class="popular-content-wp">
<div class="popular-title">
<h6><?php echo get_the_title(); ?></h6>
</div>
<div class="popular-price">
<p><?php echo wc_price($product->get_price()); ?></p>
</div>
<div class="popular-add-to-cart">
<ul>
<li>
Add to Cart
</li>
</ul>
</div>
</div>
</a>
</div>
<?php endwhile; wp_reset_query();?>
Since wordpress version 3.1 using a taxonomy slug in a WP_Query is deprecated in flavor of tax_query. Also there are some other mistakes. See WP_Query taxonomy parameters.
Your revisited code:
<?php
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 12,
'post_status' => 'publish',
'parent' => 0,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 29 ), // Term ids to be excluded
'operator' => 'NOT IN' // Excluding terms
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
// Get the instance of the WC_Product from the post ID
$product = wc_get_product( get_the_id() );
?>
<div class="popular-inner">
<a href="<?php echo get_permalink(); ?>">
<div class="popular-image d-flex ">
<div class="align-self-center mx-auto ">
<?php the_post_thumbnail(); ?>
</div>
</div>
<div class="popular-content-wp">
<div class="popular-title">
<h6><?php echo get_the_title(); ?></h6>
</div>
<div class="popular-price">
<p><?php echo wc_price( $product->get_price() ); ?></p>
</div>
<div class="popular-add-to-cart">
<ul>
<li>
Add to Cart
</li>
</ul>
</div>
</div>
</a>
</div>
<?php
endwhile;
wp_reset_postdata();
else: ?>
<p><?php _e( 'No posts found.' ); ?></p>
<?php endif;
?>
It should work now.

Custom post loop with custom taxonomy in wordpress

I am going wrong somewhere and it is driving me crazy. I am trying to pull posts from a custom post type with a custom taxonomy. I am pulling the taxonomy via an ID in the url and it just seems to show all posts of that post type no matter what I do. Here is my code:
<?php
$cruise = $_GET['reederei'];
$info = get_term_by( 'slug', $cruise, 'reederei' );
$info_id = $info->term_id;
$logo = get_field('logo', $term);
foreach ($taxes as $tax) {
$the_taxes[] = array (
'taxonomy' => 'reederei',
'field' => 'term_id',
'terms' => 'array( $info_id)',
);
}
$the_taxes['relation'] = 'OR';
$query = new WP_Query( array(
'post_type' => 'angebote',
'tax_query' => $the_taxes,
) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-sm-4">
<div class="offer_box_container offer_main">
<div class="offer_box_thumbnail"><img src="<?php the_field('featured_image'); ?>"/></div>
<h2 class="offer_inner_title"><?php the_title(); ?></h2>
<div class="offer_inner_sub highlight"><?php the_field('number_of_nights'); ?> Nächte: <?php the_field('travelling_to'); ?></div>
<div class="offer_description"><?php echo custom_field_excerpt(); ?></div>
<div class="col-sm-7 offer_button"><a class="direct_button" href="<?php the_permalink(); ?>" target="_blank">Zum Angebot >>></a></div>
<div class="col-sm-5"><div class="before_price">pro Person ab</div>
<div class="main_price"><?php the_field('price'); ?> -</div></div>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
<!-- show 404 error here -->
<?php endif; ?>
$the_taxes[] = array (
'taxonomy' => 'reederei',
'field' => 'term_id',
'terms' => 'array( $info_id)',
);
You turned your 'terms' => array() into a string. See if that helps.

Categories