Displaying post with certain key value - php

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'
);

Related

get posts order by custom field

here, i want the post order by custom field(created by plugin Advanced Custom Fields), let say the posts order by the orders(custom field), then the categories with greatest custom field value post will appear first, is it available?
here my code is,
$args = array('category' => $cat1->term_id ,
'meta_key' => 'orders',
'orderby' => 'meta_value_num',
'order' => 'DESC' ,'numberposts' => -1
);
$myposts = get_posts( $args );
$i = 1;
foreach ( $myposts as $post ) : setup_postdata( $post );
echo get_field('orders',$post->ID);
$i++;
endforeach;
Can you pass 'post_type' =>'post' in $args array. So it will be like
$args = array('category' => $cat1->term_id ,
'post_type' =>'post',
'meta_key' => 'orders',
'orderby' => 'meta_value_num',
'order' => 'DESC' ,'numberposts' => -1
);
I am assuming that you are passsing post type = post. You can change there it if it's different.

How i can get the first post of current category and echo the link?

I trying create a code that will retrieve the first post from a category and echo the link.
But, the code always getting permalink of current post. Can anyone help me to fix it?
<?php
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$array = new WP_Query(array(
'category__in' => array($category),
'post_per_page' => 1,
'order' => 'asc',
'orderby' => 'id'
));?>
<?php the_permalink($post->ID); ?>
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'ASC',
'fields' => 'ids',
'category' => 8 //or use further logic to check in list
);
$post_cus = get_posts($args);
$first_post_id = $post_cus[0];
$post_url = get_the_permalink ($first_post_id);

Wordpress custom query: 'orderby' => 'date' not working when using multiple post types

In a Wordpress page template, I set up a WP custom query which queries a custom post type named "recipe" AND the regular posts as below. This works, but 'orderby => 'date' in there doesn't work: The page first lists the regular posts ordered by date, then the recipes by date. But I need ALL of them together (i.e. mixed) ordered by date.
Here's the definition of my custom query:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...followed by the loop...]
I am grateful for any hints what I can do to achieve the desired ordering.
I found a solution myself:
It works when I add remove_all_filters('posts_orderby'); before the custom query is defined. Obviously this resets any other ordering and allows the 'orderby' => 'date' to function as expected. Complete code:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
remove_all_filters('posts_orderby'); // ADDED
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...]
Q1. what template file are you using?
The query below successfully merges the 2 post types together by date.
<?php
$args = array(
'post_type' => array('my_custom_post_type', 'post'),
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
// loop your content here
<?php include(locate_template('templates/content.php')); ?>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

Get Recent and Custom posts from same wordpress loop

I am using this code from which I am able to get 3 recent posts.
$query = new WP_Query( array( 'post_type' => 'property', 'posts_per_page' => 3, 'post_status' => 'publish', 'order' => 'DESC'));
and I am using this code to get 3 custom posts with the post__in method.
$query = new WP_Query( array( 'post_type' => 'property', 'posts_per_page' => 3, 'post_status' => 'publish', 'order' => 'DESC', 'post__in' => array( 10244, 7177, 8262)));
How can I combine them to get the 3 recent and 3 custom posts from one loop?
Any Help Appreciated. Thanks.
You can do it by merging their posts into one query.
f.e.
$merged_query = new WP_Query();
$merged_query->posts = array_merge( $query1->posts, $query2->posts );
Example:
<?php
$merged_query = new WP_Query();
$merged_query->posts = array_merge( $query1->posts, $query2->posts );
while ( $merged_query->have_posts() ) : $merged_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

Posts by admin on the top from all the post?

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
);

Categories