I want to add conditional statement in author. something like if the author is an admin set this to null. is this possible?
$args = apply_filters( 'job_manager_get_dashboard_jobs_args', array(
'post_type' => 'job_listing',
'post_status' => array( 'publish', 'expired', 'pending' ),
'ignore_sticky_posts' => 1,
'posts_per_page' => $posts_per_page,
'offset' => ( max( 1, get_query_var('paged') ) - 1 ) * $posts_per_page,
'orderby' => 'date',
'order' => 'desc',
'author' => get_current_user_id()
));
I assume that get_current_user_id() is getting an id for author.
Change your code from this:
'author' => get_current_user_id()
With this:
'author' => ((get_current_user_id() == 'admin') ? NULL : get_current_user_id())
I think you can use this sample code.
<?php
$array = array('test' => testFunction());
function testFunction()
{
// do your condition here
// return 'a';
}
print_r($array);
?>
Related
On a used car dealership website, I have a PHP Query that displays all results for the relevant vehicle model. So if you visit the "Honda Civic" page it will display all the vehicles listed as a Civic.
Currently, I'm using the following query:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array('vehicle'),
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 10,
'paged' => $paged,
'meta_key' => 'model',
'meta_value' => $model,
);
This works as expected, however I would like to sort the result so that it displays used vehicles first and demo vehicles second. So I need to order the results by the "Demo" meta key, while still displaying only the relevant models.
I've tired the query below based on another StackOverflow question's answer, but it returns no results:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array('vehicle'),
'post_status' => 'publish',
'meta_key' => 'Demo',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
array('key' => 'model', 'value' => '$model')
)
);
Any help would be appreciated.
Thanks in advance
Willem
use below query argument for meta key and value.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array('vehicle'),
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'model',
'value' => $model,
'compare' => '='
),
),
'meta_key' => 'Demo',
'orderby' => 'meta_value', //meta_value_num or meta_value
'order' => 'ASC'
);
?>
I'm trying to order a wp_query that includes a meta_query...
After some different approaches, it seems that the query can't be ordered since I include the meta_query, and orderby => ID is used by default (I think) and it can't be overwritten. Any ideas on how to accomplish this?
Here's the PHP code I use just to display the posts by meta value:
$paged = get_query_var('paged') ? get_query_var('paged') : (get_query_var('page') ? get_query_var('page') : 1);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'subtitrare',
'value' => 'romana',
'compare' => 'LIKE'
)
)
);
$listing_query = null;
$listing_query = new WP_Query($args);
if ($listing_query->have_posts()) : get_template_part('loop-item');
endif;
wp_reset_postdata();
As you can see I also paginate the results, so i can use the same $args. I've tried to use a custom function to rearrange the results but it didn't worked for me.
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'orderby' => 'modified',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'subtitrare',
'value' => 'romana',
'compare' => 'LIKE'
)
)
);
This is the code you need but you already said that you tried this. This will return all the posts which have the custom field value set to LIKE 'romana'. What are the results you get from using this code?
I am making a function in a plugin and the function will delete the database row when the post move to the trash bin. However, I cannot get the post_id using get_posts().
Here is my code:
function delete_condition($post)
{
global $wpdb;
$allposts = get_posts(array(
'numberposts' => -1,
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'job',
'suppress_filters' => true));
foreach( $allposts as $postinfo ) {
$wpdb->delete('rule', array('post_id' => $postinfo));
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
thanks
The action hook you're using here, wp_trash_post, passes the $post_id to the function as a parameter. See: https://codex.wordpress.org/Plugin_API/Action_Reference/trash_post
It sounds like you want to delete all rows from one table that have the same post ID as the one being trashed.
I think you might want to write something like this:
function delete_condition( $post_id ) {
global $wpdb;
// Delete rows in the rule table which have the same post_id as this one
if ( 'job' === get_post_type( $post_id ) ) {
$wpdb->delete('rule', array('post_id' => $post_id ) );
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
$postinfo is the object. You want only the ID of post. So you should write $postinfo->ID. Replace your loop with the below -
foreach( $allposts as $postinfo ) {
$postinfoID = $postinfo->ID;
$wpdb->delete('rule', array('post_id' => $postinfoID));
}
<?php
function delete_condition($post)
{
global $wpdb;
$allposts = get_posts(array(
'numberposts' => -1,
'post_status' => 'any',
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'job',
'suppress_filters' => true));
foreach( $allposts as $postinfo ) {
$wpdb->delete('rule', array('post_id' => $postinfo));
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
?>
I have a custom post type which has an ACF check box to define if an post is featured or not. I wanted to show 6 featured and 6 non featured posts on a page and so created 2 WP_Query loops both with separate args. Whilst the separation of featured and non-featured is working, the page only shows 6 posts in total and I'm not sure how to resolve that?
My code:
<?php
$args1 = array(
post_type => 'fairs',
posts_per_page => -1,
showposts => 6 ,
meta_key => 'start',
orderby => 'meta_value_num',
order => 'ASC'
);
$new1 = new WP_Query($args1);?>
<?php while ($new1->have_posts()) : $new1->the_post();?>
<?php $field = get_field( 'wa_feature', get_the_ID() ); if ( true == $field ):?>
Featured Posts
<?php endif;?>
<?php endwhile; wp_reset_postdata();?>
<?php
$args2 = array(
post_type => 'fairs',
posts_per_page => -1,
showposts => 6 ,
meta_key => 'start',
orderby => 'meta_value_num',
order => 'ASC'
);
$new2 = new WP_Query($args2);?>
<?php while ($new2->have_posts()) : $new2->the_post();?>
<?php $field = get_field( 'wa_feature', get_the_ID() ); if ( false == $field ):?>
Non-featured Posts
<?php endif;?>
<?php endwhile;?>
Your WP_Query isn't right. You do two completely identical queries ($args1 = $args2), and expects diffrent results from them. Also you put meta_key into query arguments, but without telling what and how to compare.
Depending on what type you have chosen, the right syntax, if your meta filed is named featured and the query can be:
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => true //or 1, or 'yes` depending of ACF type you have choose
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => false//or 0, or 'no`
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
or if this featured field exists only in featured posts then
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC'.
'meta_query' => array(
array(
'key' => 'featured',
'compare' => 'EXISTS',
),
)
);
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC'.
'meta_query' => array(
array(
'key' => 'featured',
'compare' => 'NOT EXISTS',
),
)
);
Check ACF query or Codex for the right sintax.
Your arguments should be quoted:
array(
'post_type' => 'fairs',
'posts_per_page' => 6,
'meta_key' => 'start',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
Array keys can either be integers, or strings. Read more in the PHP array() docs.
You should also only use posts_per_page in this case (and remove showposts, which was replaced by posts_per_page in WP 2.1).
I have this wp query...
$downloads = new WP_Query(array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
));
But I want to add this to the query if my $user_admin condition is true...
if ($user_admin)
$downloads = new WP_Query(array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
)
);
So I run this it seems to break my loop, but not cause a fatal error...
$downloads = new WP_Query(array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
));
if ($user_admin) {
$downloads = new WP_Query(array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
));
}
OK my question is essentially this... How do I blend the two $downloads variables if the $user_admin condition equals true.
But the fastest and correct method of actually going about doing this as my method does not work.
From looking at your code it looks like you could merge the array together and then create the new WP_Query object. From your description I understand that you are saying that you want the queries to be blended into one and not the results of the query blended into one.
$args = array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
);
if ($user_admin) {
$args = array_merge($args, array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
));
}
$downloads = new WP_Query($args);
I also was wondering you indicated that your current code seems to break your loop. Exactly what is happening with your first code example. Are you getting a blank page or simply not having any articles returned?
Another thing to note when checking if a user is an administrator you can also use is_admin() instead of $user_admin.
Function Reference/is admin
Try:
$query = array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
);
if ($user_admin) {
$query2 = array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
);
$query = array_merge($query, $query2);
}
$downloads = new WP_Query($query);
Hope this helps!