Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
How to make my code more compact and beautiful. I think that it is possible to shorten it more, but unfortunately I do not know at all how. I hope that you will help me, dear specialists.
<?php
$postidid = get_the_ID();
$args = array(
'orderby' => 'rand',
'showposts' => 10,
'cat' => '-47,-56',
'post__not_in' => array( $postidid),
'meta_query' => array( array('key' => 'expost', 'compare' => 'NOT EXISTS'))
);
$args1 = array(
'orderby' => 'rand',
'showposts' => 10,
'cat' => '76',
'post__not_in' => array( $postidid),
'meta_query' => array( array('key' => 'expost', 'compare' => 'NOT EXISTS'))
);
$args2 = array(
'orderby' => 'rand',
'showposts' => 10,
'cat' => '34',
'post__not_in' => array( $postidid),
'meta_query' => array( array('key' => 'expost', 'compare' => 'NOT EXISTS'))
);
$news = new WP_query();
if( in_category(76)) {
$news->query($args1);
} elseif (in_category(34)){
$news->query($args2);
} else {
$news->query($args);
}
?>
<ul class="list" style="text-align:center;"><?php while ($news->have_posts()) : $news->the_post(); ?>
<li><img src="<?php echo get_post_image();?>" class="thumbnail" alt="<?php the_title_attribute(); ?>"><br><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
Sorry for My English
You can try this way. It can help you.
<?php
$postidid = get_the_ID();
$args = array(
'orderby' => 'rand',
'posts_per_page' => 10,
'post__not_in' => array( $postidid ),
'meta_query' => array(
array(
'key' => 'expost',
'compare' => 'NOT EXISTS'
)
),
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( - 47, - 56 )
)
)
);
$news = new WP_Query();
if ( in_category( 76 ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 76 )
)
);
$news->query( $args );
} elseif ( in_category( 34 ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 34 )
)
);
$news->query( $args );
} else {
$news->query( $args );
}
if ( $news->have_posts() ):?>
<ul class="list" style="text-align:center;">
<?php while ( $news->have_posts() ):
$news->the_post(); ?>
<img src="<?php echo get_post_image(); ?>" class="thumbnail" alt="<?php the_title_attribute(); ?>"><br><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
endif;
Related
I have searched high and low and applied all of the solutions I've found here on Stackoverflow with no luck so trying a post to see if someone could assist me...
I am trying to display the posts that sit under a category in my custom post type taxonomy.
The CPT is called "Fabrics" and the taxonomy is called "Type" - Under "Type" I have a category called "Lycra"
I want to display all of the posts that exist under "Lycra"
Here is the code I have so far, but it just lists all of the posts under Fabrics:
<?php
$cat_terms = get_terms(
array(
'post_type' => 'fabrics',
'post_status' => 'publish',
'posts_per_page' => 9999999,
'orderby' => 'date',
'order' => 'DES',
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => 'lycra',
),
),
)
);
if( $cat_terms ) :
echo '<ul class="fabric-listing">';
foreach( $cat_terms as $term ) :?>
<li class="each-fabric">
<a href="<?php echo get_term_link( $term );?>">
<img src="<?php the_field('imagecat', $term); ?>">
<div class="term-name">
<?php echo $term->name; ?>
</div>
</a>
</li>
<?php
wp_reset_postdata();
endforeach;
echo '</ul>';
endif;
?>
What am I missing here? Any help is super appreciated
Use get_posts and not get_terms
$cat_terms = get_posts(
array(
'post_type' => 'fabrics',
'post_status' => 'publish',
'numberposts' => -1,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => 'lycra',
),
),
)
I am working in my first PHP project and I don't understand what does this piece of code does. May anyone help me please?
I don't work very well with php.
<?php
$args = array(
'post_type' => 'manual-pdf',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
// ['relation'] => 'OR', // use this for a different comparison
array(
'key' => 'wpcf-category',
'value' => 2,
'compare' => '='
),
array(
'key' => 'wpcf-version',
'value' => 1,
'compare' => '='
)
));
query_posts($args);
if ( have_posts() ) :
while (have_posts()): the_post();
?>
<a target="_blank" href="<?=$url = types_render_field("url", array('raw' => 'false'));?>"><? the_title(); ?></a><br><?=$descricao = types_render_field("description", array('raw' => 'false'));?><hr>
<?
endwhile;
else :
endif;
wp_reset_query();
?>
Basically, this part made me confused mostly:
<a target="_blank" href="<?=$url = types_render_field("url", array('raw' => 'false'));?>"><? the_title(); ?></a><br><?=$descricao = types_render_field("description", array('raw' => 'false'));?><hr>
How to do with this code only show the "related posts by category"?
This code is inside the "single.php"
I did not stackoverflow but could not find a solution. Can anyone help me?
<?php
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'slug',
'terms' => 'top-audio'
)
)
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post();
?>
<h3><?php the_title();?></h3>
<?php endwhile; wp_reset_postdata();?>
First, you need to grab current post terms using this function
get_the_terms ( get_the_id(), 'your_custom_taxonomy' );
then you grab the terms slug for tax_query in your WP_Query.
$postsPerPageq = 5;
$terms_slugs = array();
//get the current post terms slug
$terms = get_the_terms( get_the_id(), 'audiotop' );
foreach ( $terms as $term) {
$terms_slugs[] = $term->slug;
}
//WP_Query args
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'slug',
'terms' => $terms_slugs,
)
)
);
i hope it your solve your problem.
Add category name in your allargw array value..
<?php
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'category_name'=>'Your category name',
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'slug',
'terms' => 'top-audio'
)
)
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post();
?>
<h3><?php the_title();?></h3>
Hope this helps to you!!
Updated answer:
If you want add category automatically, use like this..
wp_set_object_terms( 'your_post_id', 'your_category_name', 'your_taxonomy_name');
You can try this..
if you want to retrieve posts by simple posts category then check this code:
<?php
$category = get_the_category($post->ID);
$cat_id = $category[0]->term_id;
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'cat' => $cat_id
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post(); ?>
<h3><?php the_title();?></h3>
<?php endwhile; wp_reset_postdata();?>
else if you want to retrieve posts by custom taxonomy then check the code below:
<?php
$term_list = wp_get_post_terms($post->ID, 'audiotop', array("fields" => "all"));
$term_id = $term_list[0]->term_id ;
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'id',
'terms' => $term_id
)
)
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post(); ?>
<h3><?php the_title();?></h3>
<?php endwhile; wp_reset_postdata();?>
Hope, this will help you.
I'm looking to make the code below sort my custom posttype (Product Type).
So that my active page will only show the posts with the term 'Oil', rather then 'Fuel' and 'Tires'.
$args = array(
'post_type' => 'Products',
'posts_per_page' => -1
);
$my_query = new WP_Query( $args );
if ($my_query->have_posts()) {
while($my_query->have_posts() ) {
echo '<div class="custom-post">';
$my_query->the_post();
echo '<p>' . the_content() . '</p>';
the_post_thumbnail();
echo '</div>';
}
wp_reset_postdata();
}
// Edit, as a reply to the 3 below answers
I assume the foreach loop is a replacement for the WP_Query method, but the tax_query was new to me, and now that I know it exists I looked it up in the codex, however it still wont work. I honostly believe it is something as simple as me naming something in the tax_query wrongly, so I will show my taxonomy code here:
function my_custom_taxonomies() {
register_taxonomy(
'product-type',
'products',
array(
'label' => 'Type of Product',
'rewrite' => array( 'slug' => 'race-products' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'my_custom_taxonomies' );
And the change to the $args:
$args = array(
'post_type' => 'Products',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product-type',
'field' => 'slug',
'terms' => 'oil'
),
),
);
Try the below code:
<?php
$myposts = get_posts(array(
'showposts' => -1,
'post_type' => 'Products',
'tax_query' => array(
array(
'taxonomy' => 'products_cat',
'field' => 'slug',
'terms' => 'oil'
);
)
)
);
foreach ($myposts as $mypost) {
echo $mypost->post_title . '<br/>';
echo $mypost->post_content . '<br/>';
echo get_the_post_thumbnail( $mypost->ID );
}
?>
$loop = new WP_Query(array(
'posts_per_page' => 10,
'post_type' => 'product_type',
'order' => 'DESC',
'orderby' => 'id',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'your_texonomy',
'field' => 'slug',
'terms' => 'Oil',
),
),));
You need to try the following parameters in query.
$args = array(
'post_status' = 'publish',
'tax_query' = array(
'taxonomy' => 'categories',
'field' => 'id',
'term' => 'category id here'
)
);
$the_query = wp_query($args);
IN my wp site, I have 2 category and a few post like that..
cat_1- post 1, post 2, post 3.
cat_2- post 2, post 3, post 4.
When i am in post 3 page, i want to show releted article only from category 2.here is my code:but it returns empty. Probably i did not catch the logic. Any help would be highly appreciated.
<?php
$terms = get_the_terms( $post_id, 'category' );
if( empty( $terms ) ) $terms = array();
$term_list = wp_list_pluck( $terms, 'slug' );
$related_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'post__not_in' => array( get_the_ID() ),
'orderby' => 'desc',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term_list
),
array(
'taxonomy' => 'category',
'terms' => array('cat_1'),
'field' => 'slug',
'operator' => 'NOT IN',
),
),
);
$related = new WP_Query( $related_args );
if( $related->have_posts() ):
?>
<div class="post-navigation">
<h3>Related posts</h3>
<ul>
<?php while( $related->have_posts() ): $related->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
wp_reset_postdata();
?>
here is the following query ...may be helpful to you...
$custom_tex=array(array('taxonomy'=>'category','field'=>'slug','terms'=>'cat_2'));
$new = new WP_Query(array('post_type'=>'post','posts_per_page'=>-1, 'tax_query'=>$custom_tex,'order' => 'DESC','orderby' => 'ID' ));
while ($new->have_posts()) : $new->the_post();
echo $post->post_title;
echo "<br>";
endwhile;