I show my last post in a page with this code:
$query1 = new WP_Query();
$query1->the_post();
and it further with:
$id = $query->ID;
to retrive last post ID
so I wrote a new wp_query and I want to exclue that ID from the results:
I wrote this but it don't work:
$query2-> new WP_Query('p=-$id');
what's the problem?
You haven't excluded anything. Read the Codex. p= includes posts. It does not exclude them. What you need is post__not_in
$query2-> new WP_Query(array('post__not_in' = array($id)));
My code is works fine:
$ID =array('1,2,3,4,5');
$news = new WP_Query(array('
'post_type' => 'post',
'showposts' =>3,
'order' => 'DESC',
'post__not_in' => $ID
));
if ( $news->have_posts() ) :
echo '<div>';
while ( $news->have_posts() ) : $news->the_post(); ?>`
//Your code here
Related
Is there anyway to search posts by slug through XMLRPC
https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPosts
getPosts() doesn't seem to return using "name"..
$args = array(
'name' => 'my-slug',
'number' => 1
);
$post = $wpClient->getPosts( $args );
Please let me know if there is a workaround for this, I need to search by slug and then update those slugs remotely via XMLRPC. cheers
I ended up using Methods, this may help someone and save time.. paste the following code in functions.php of the domain you are fetching data from
add_filter('xmlrpc_methods', 'clx_xmlrpc_methods');
function clx_xmlrpc_methods($methods) {
$methods['getPostBySlug'] = 'clx_getpost';
return $methods;
}
function clx_getpost($args) {
global $wp_xmlrpc_server;
$slug = $args["slug"];
$pargs = array(
'name' => $slug,
'post_type' => 'post',
'numberposts' => 1
);
$my_posts = get_posts($pargs);
if( $my_posts ) :
return $my_posts; //echo $my_posts[0]->ID;
endif;
}
from your XMLRPC code use the following to get POST array from slug
$args = array(
'slug' => 'your-post-slug'
);
$postArray = $wpClient->callCustomMethod( 'getPostBySlug', $args );
I have a WordPress query like this :
<?php $recent = new WP_Query( array( 'tag' => $tags, 'posts_per_page' => '4' ) ); while( $recent->have_posts() ) : $recent->the_post(); ?>
Which gives me the last 4 posts using the $tags.
But any idea of how may I edit this code to do not get the four first posts but from the 4th?
Thanks!
Take a look at the WP_Query Documentation. It goes into detail on how to properly interact with the class.
If you want to query your posts starting from the 4th post, you'll want to take a look at the offset parameter. In your case, take a look at the code below (Note: I moved the arguments array to a variable for clarity)
$recent_args = array(
'tag' => $tags,
'posts_per_page' => 4, // Don't need quotes around integers
'offset' => 3, // Add this param to "Skip" this many posts
);
$recent = new WP_Query( $recent_args );
// Loop through your posts here
If you need to get all posts beyond the first (newest) posts, you can use the offset argument:
<?php $next_posts = new WP_Query( array(
'tag' => $tags,
'offset' => 4,
)
);
while($next_posts->have_posts()) : $next_posts->the_post(); ?>
There is a caveat to this! If you need pagination or set posts_per_page to -1, this will not work. For more information on a more robust solution if you need pagination, checkout the WP documentation:
https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
So here is the code that worked for me :
<?php $recent = new WP_Query(array( 'tag' => $tags, 'posts_per_page' => '4', 'offset' => '3', )); while($recent->have_posts()) : $recent->the_post(); ?>
Thanks a lot to #disinfor & #Xhynk for leading me to the right direction !
I have a function to grab products by their category and return specific data about them using their category slug like so:
$itemArgs = array(
'post_type' => 'product',
'posts_per_page' => 1000,
'product_cat' => $request['id'],
'include_children' => false
);
$loop = new WP_Query( $itemArgs );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
//DO STUFF
endwhile; endif;
Which works fantastically. What I need now is to get a product by its own slug. Where the one above might get every product for "Cookies" category, this one should return just the "Chocolate Chip" product.
I've tried replacing 'product_cat' with 'slug' and 'product_slug' but those don't appear to work. This seems like a fairly straightforward thing to do... there's documentation on finding a product by 48 different properties... slug is not one for some reason. I just get the entirety of the product collection returned.
If you look at the Wordpress codex post about the WP_Query You will see a part which talks about Page and Posts Parameters. There you will see you can use the name parameter.
Assuming your product slug is chocolate-chip, you can use this to retrieve your product with the post_type and name parameters:
$itemArgs = array(
'post_type' => 'product',
'name' => 'chocolate-chip'
);
$query = new WP_Query($itemArgs);
$product_obj = get_page_by_path( $slug, OBJECT, 'product' );
https://wordpress.stackexchange.com/questions/206886/get-product-details-by-url-key-in-wordpress-woocommerce
I'm inside a Custom Post Type template and I want to make a new WP_Query to display a different Custom Post Type post using the same category of the current one. I know I can just put it as a string but I need to make it dynamic for my CPT template.
For example:
<?php
$args = array (
'post_type' => 'new_cpt_to_display',
'category_name' => 'category_of_the_current_cpt' );
$new_cpt_query = new WP_Query($args);
?>
I hope I explained it well.
You can combine get_the_category() which returns an array of category IDs for the current post and the category__in (results for each of the categories) or category__and (results in all the categories) arguments for WP_Query.
$category_ids = get_the_category();
$args = array(
'post_type' => 'new_cpt_to_display',
'category__in' => $category_ids
);
$new_cpt_query = new WP_Query( $args );
Solved!
Got an idea from the previous answer.
<?php
$cat = get_the_category();
$cat_name = esc_html( $cat[0]->name );
$args = array (
'post_type' => 'new_cpt_to_display',
'category_name' => $cat_name
);
$new_cpt_query = new WP_Query($args);
?>
I have a category, where posts are listed by this wp_query:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'stage' => 'process',
'paged' => $paged,
);
$query = new WP_Query( $args );
Using plugin m_snap (alphabetical order of posts) there is a problem. It provides custom field for any post with a letter. So, the URL becomes something like this:
[...]/category/product/letter/A/ but has no difference with [...]/category/product/
How can I correct wp_query to get proper list of posts using m_snap?
UPD. Found that it's something wrong with rewriting urls. This is code from plugin:
function m_snap_generate_rewrite_rules($wp_rewrite) {
$IIIIIIIII1ll = array (
'letter/(.+)' => 'index.php?letter=' . $wp_rewrite->preg_index(1),
'tag/(.+?)/letter/(.+)' => 'index.php?tag='.$wp_rewrite->preg_index(1).'&letter='.$wp_rewrite->preg_index(2),
'category/(.+?)/letter/(.+)' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&letter='.$wp_rewrite->preg_index(2)
);
$wp_rewrite->rules = $IIIIIIIII1ll + $wp_rewrite->rules;
}
...
add_action ( 'generate_rewrite_rules', 'm_snap_generate_rewrite_rules' );
Any ideas to correct?