Need to show negitive results from search (Wordpress Plugin) - php

I've been working on showing a list of posts that users have NOT favoured using this wordpress plugin: https://en-gb.wordpress.org/plugins/favorites/
I've re-created a search that I can make into a custom shortcode, but I can't work out how to reverse it to show UNfavorited lists.
Also, I would like to show the title and have the title link to the page - can anyone help? This is what I have:
$favorites = get_user_favorites();
if ( $favorites ) :
$favorites_query = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post__in' => $favorites
));
if ( $favorites_query->have_posts() ) :
while ( $favorites_query->have_posts() ) : $favorites_query->the_post();
echo '<p>' . get_the_title( $favorite ) . '</p>';
endwhile;
endif;
wp_reset_postdata();
endif;

I wonder if you mean:
$favorites_query = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post__not_in' => $favorites
));
where we use post__not_in instead of post__in ?
From the Codex:
post__not_in (array) - use post ids. Specify post NOT to retrieve. If
this is used in the same query as post__in, it will be ignored.
But note that the amount of returned posts could be large with posts_per_page as -1.

Related

WordPress - Shortcode for Custom Post Type Category

Hej, i want to Display all Posts from one Category of my Custom Post Type with a Shortcode.
Example:
My-Custom-Post-Type:
Tomatoe, Lettuce, Fruit, Vegan, Medium Rare, Rare
Food-category:
Burger, Pizza, Salad
Burger:
Vegan, Medium Rare, Rare
Salad:
Tomatoe, Lettuce, Fruit
Is there a way to do this?
Sorry for bad Example
From your example I think you have confused CPTs with Taxonomies and Terms, but in general all you have to do is first create a custom shortcode by adding this in your functions.php:
function your_custom_function ( $atts ) {
//Run your Query
//Iterate and display output
return $output;
}
add_shortcode( 'your_shortcode', 'your_custom_function' );
Then inside your function you would need a query to get and display the posts you want, example:
$args = array(
'post_type' => 'my_custom_post_type',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'cat' => 3,
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li'> . the_title() . '</li>';
endwhile;
wp_reset_postdata();
This tool can also help you with your custom query.

WP Sorting custom posts alphabetically by Title

Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>
<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.

Limit posts per page after using array_merge in Wordpress

I have two post types I want to display, Posts and then a Custom Post Type called 'Notes'. I want to query both of these and display them together. I've currently got it working using array_merge.
I want to create a new query so I can choose how many posts to display per page and also get pagination working. I've tried various different things to limit the amount of posts displayed but can't seem to crack it.
Here is my code:
$q1_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q1_posts = get_posts( $q1_args );
// get the posts for the second query
$q2_args = array(
'post_type' => 'notes',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q2_posts= get_posts( $q2_args );
// Merge the post arrays together, and sort by date using the order_by_date function
$final_posts = array_merge( $q1_posts, $q2_posts );
usort( $final_posts, 'order_by_date' );
// Loop over the posts and use setup_postdata to format for template tag usage
foreach ( $final_posts as $key => $post ) {
$post_type = $post->post_type;
setup_postdata( $post );
//DO STUFF
}
Any thoughts on how I can limit posts per page and get pagination working?
Is there any particular reason this can't be done like this?
$args=array(
'post_type' => array('post', 'notes'),
'posts_per_page' => 15, //or any other number you want per page
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => (( get_query_var('page') ) ? get_query_var('page') : 1)
);
$posts=get_posts($args);
if ($posts->have_posts())
{
while ($posts->have_posts())
{
$posts->the_post();
//DO STUFF
}
//add pagination here
}
else
{
// no posts found
}
wp_reset_postdata();

Woocommerce get product ID's from Category

So on my template for taxonomy-product_tag.php, I want to get all product id's from the Category.
Here is how I currently do it
<?php
$post_ids = array();
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'dog-collars', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$post_ids[] = get_the_ID();
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_query();
print_r($post_ids);
?>
I can loop through the product_cat, pull id's into an array and then further down the page I use foreach and the WC product factory to manipulate data how I want it shown for users.
My problem is I need the loop to be Dynamic based on categories, and I can't understand how to do this.
I did think I can just grab the category name from the url
<?php $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
Grab it and the parse to just get the last , i.e category name, and then print into loop
But this seems like it would be a really poor way of doing it.
What I want is in the args
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'DYNAMICHERE', 'orderby' => 'rand' );
I want to be able to populate product_cat dynamically based on the category I am on
Any help or advise / pointing me in the right direction would be appreciated
Use get_query_var( 'product_cat' ).

WP Loop with custom array field as argument

I'm trying to do a query with the arguments below. Somehow, WP just doesn't return any posts. I can't figure out what I'm doing wrong! Any help?
Some more info: I have a custom post type that contain featured images. I want them to be displayed in a header slider. Through Advanced Custom Fields plugin I've created a custom field in the posts: 'assigned_page'. It's an array with page ID's on which that specific slide should be displayed. '$current_page' is the ID of the current page that's to be displayed. So, $args should filter the custom post type, and the posts that have the current page ID in their 'assigned_page' array.
// Get the current page ID
global $post;
$current_page = $post->ID;
$string_page = (string)$current_page;
$current_parent_page = $mv_is_subpage->ID;
// Post selection
$args = array (
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'meta_query' => array(
array(
'meta_key' => 'assigned_page',
'meta_value' => $string_page,
)
),
);
Then:
$query = new WP_Query( $args );
And then the loop:
while ($query->have_posts()) : $query->the_post();
I guess it should be like this (according to docs at http://codex.wordpress.org/Class_Reference/WP_Query) :
array(
'key' => 'assigned_page',
'value' => $string_page,
)

Categories