Posts by admin on the top from all the post? - php

I am retrieving some post and to show post on first come first basic. Now I want to show those post on the top which are posted by admin and the rest post by non admin will come after admin posts.
the code I am using in php is:
$tit = get_the_title();
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit ,
);
$slides = get_posts($args);
?>
<ul id="myList">
<?php foreach($slides as $post) : setup_postdata($post); ?>
<li>the post will go here</li>
<?php endforeach; wp_reset_postdata(); ?>

To display posts from admin first and then from non-admin, you have to call get_posts twice.
Once with :-
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit ,
'author' => '123' // where 123 is ID of your admin author
);
The other will be :-
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit ,
'author' => '-123' // display posts except admin author
);

Add the author method to your array:
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit,
'author_name' => 'Administrator' //add this, change Administrator to your name
);

Related

Get Related Post on Custom Post type on Wordpress

please help im new in web development i dont know what to put on fields and ids i already had custom post type. i want to show the custom post type on normal post related topic its specific post...
<?php
//get the taxonomy terms of custom post type
$customTaxonomyTerms = wp_get_object_terms( $post->ID, 'your_taxonomy', array('fields' => 'ids') );
//query arguments
$args = array(
'post_type' => 'profile',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'profile',
'field' => 'id',
'terms' => $customTaxonomyTerms
)
),
'post__not_in' => array ($post->ID),
);
//the query
$relatedPosts = new WP_Query( $args );
//loop through query
if($relatedPosts->have_posts()){
echo '<ul>';
while($relatedPosts->have_posts()){
$relatedPosts->the_post();
?>
<li><?php the_title(); ?></li>
<?php
}
echo '</ul>';
}else{
//no posts found
}
//restore original post data
wp_reset_postdata();
?>

Show pagination and exclude category

So, I am trying to show items from a specific category in the woocommerce:
Here is what I have so far:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => '', 'orderby' => 'date', 'order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
<div class="content"> Content </div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
When the product_cat is empty, it shows all the items. I want to include "exclude category".
For example, I want to show all but items in a "no_good" category.
Could someone help me out with it?
Also, how can I add a pagination to this?
Thanks!
Is product_cat your custom taxonomy? If it is then you need to modify your $args with tax query:
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'tax' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => array('no_good'),
'operator' => 'NOT IN',
),
),
);
...
This assumes that "no_good" is a product_cat name. Adjust field if it isn't.
Regarding the pagination part, do check the codex article regarding pagination.

Get permalink of the oldest post in wordpress

I have kept a Prev-Next option in the single post page to navigate though the posts and this is what I'm using for the next button.
<?php echo get_permalink(get_adjacent_post(false,'',false)); ?>
But I can't figure out a way to link this same button to the very first posts when there are no more new posts to show. The reason is that the posts are used to show products.
Please note: To link Prev button in oldest post to the newest post I have used used this code.
<?php $next_page=get_permalink(get_adjacent_post(false,'',true));
$current_page=get_permalink();
if($next_page==$current_page){
$args = array( 'numberposts' => '1', 'category' => CAT_ID );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo get_permalink($recent["ID"]);
}
} else {
echo $next_page;
}
?>
add the args to query whatever posts are part of the products.
$posts_array = get_posts( $args );
get_permalink($posts_array[0]->ID); // First posts;
$args should be something like this (make sure it returns all the products posts):
$args = array(
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'ASC',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);

Woocommerce show product

I am a little confused as to that the issue is here, I am trying to pick out a specific product by doing this:
<?php
ini_set('max_execution_time', 0); //I saw maximum execution time error on your image - this is for that
$args = array(
'post_status' => 'publish',
'post_type' => 'product',
'meta_value' => 'yes',
'posts_per_page' => 10,
'product_cat' => 'grammar'
);
$product_query = new WP_Query( $args );
?>
<?php while ( $product_query->have_posts() ) : $product_query->the_post(); global $product; ?>
<?php the_title(); ?>
<?php echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt ); ?>
<?php endwhile; ?>
But nothing is being produced. So I am creating a product under the relevant category and placing content into the description in both main and product short description but still nothing shows?
The error is probably 'meta_value' => 'yes',. You need to specify a meta_key as well.
$args = array(
'post_status' => 'publish',
'post_type' => 'product',
'meta_key' => 'my_meta_key',
'meta_value' => 'yes',
'posts_per_page' => 10,
'product_cat' => 'grammar'
);
I have no idea what the meta key should be so change my_meta_key into what you want. Also make sure that the the value for product_cat is correct. It should be the slug of the category.

Displaying post with certain key value

I have a wordpress function that displays all posts of a custom meta.
PHP:
<?php
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
echo get_the_ID();
endwhile;
?>
This displays 4 posts per page. However, I only want to display those posts whose $key value is dogs.
Hope this helps.
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc',
'meta_value' => 'dogs'
);

Categories