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?
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 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 am using WordPress paginate_links() function in my custom template but the problem is when ever i go to page 2 the page stucks and it gives 404 error in console. My custom query is
if( get_query_var( 'paged' )){
$paged_documents = get_query_var( 'paged' );
}
else if( get_query_var( 'page' )){
$paged_documents = get_query_var( 'page' );
}
else{
$paged_documents = 1;
}
$query = new WP_Query( array('post_type' => 'documents', 'posts_per_page' => 8,'paged'=>$paged_documents) );
My permalink is set to Post name.
I have already found two solutions.
1) Change permalink settings to "Plain". Which i dont want to.
2) Page name and url must be different from custom post type. For Example in above snippet post_type is document as well as page name and url. I dont want to change the page name and urls.
Try this:
https://developer.wordpress.org/reference/functions/get_query_var/
<?php
wp_reset_query();
$paged_documents = get_query_var('paged', 1);
$query = array(
'post_type' => 'documents',
'posts_per_page' => '8',
'paged' => $paged_documents
);
?>
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 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